0

I have menu xml like this:

<root>
    <item name="submenu" href="">
        <item name="delete post" href="example.com/delete" />
        **<item name="delete post" href="" />**
        **<item name="add post" href="" />**
        <item name="add post" href="example.com/add" />
        **<item name="add post" href="" />**
        <item name="do not remove" href="" />        
    </item>
    <item name="list post" href="example.com/list" />
    **<item name="list post" href="" />**
    <item name="remove only one of these" href="" />
    **<item name="remove only one of these" href="" />**
</root>

I need to remove duplicate items and found a solution here: efficiently removing duplicate xml elements in c#

My problem is if there is href filled I want to keep this exact record - so I need to sort them after grouping but before removing a record.

So I need to remove records marked with ** but can't get a way to make OrderBy(string.IsNullOrWhiteSpace) work with GroupBy.

Any help appreciated.

tester.one
  • 369
  • 2
  • 6
  • 23

2 Answers2

1

Is this what you're looking for?

public class Item
{
    public string Name { get; set; }
    public string Href { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        var data = new List<Item>
        {
            new Item { Name="1", Href="" },
            new Item { Name="1", Href="b" },
            new Item { Name="2", Href="c" },
            new Item { Name="2", Href="" },
            new Item { Name="2", Href="" },
        };

        var results = data.OrderBy(d => d.Name).ThenByDescending(d => d.Href).GroupBy(d => d.Name).Select(g => g.First());

        foreach (var result in results)
        {
            Console.WriteLine(result.Name + ", " + result.Href);
        }

        Console.ReadLine();
    }
}

Results are:

1, b

2, c

Pierre-Loup Pagniez
  • 3,611
  • 3
  • 29
  • 29
1

This is not a simple issue do to the recursive nature of the xml to work under all conditions. You need to use a recursive method like the code below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication67
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            XElement root = doc.Root;
            RemoveRecursive(root);
        }

        static void RemoveRecursive(XElement element)
        {
            var groups = element.Elements()
                .GroupBy(x => new { tagname = x.Name.LocalName, itemname = (string)x.Attribute("name") })
                .Select(x => x.Select(y => new { element = y, index = (string)y.Attribute("href") == "" ? 0 : 1 })).ToList();

            foreach(var group in groups)
            {
                var orderGroup = group.OrderByDescending(x => x.index).ToList();
                for (int i = orderGroup.Count() - 1; i > 0; i--)
                {
                    orderGroup[i].element.Remove();
                }
            }
            foreach(XElement child in element.Elements())
            {
                if (child.HasElements) RemoveRecursive(child);
            }
        }
    }
}
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • It works! I still need to test to understand fully what is happening in here through :) – tester.one Sep 19 '18 at 08:25
  • 1
    I'm grouping by tag name and attribute name. Then I'm give the number index 0 to href being empty and 1 to href not being empty. Then sorting by index descending. I then remove items in reverse order leaving the fir item. The code use recursion so I delete at each descendant. – jdweng Sep 19 '18 at 10:45