Eran Meir February 2016

Remove duplicated items from XML by an attribute

Trying to delete <shipmentIndex Name=\"shipments\">whatever...</shipmentIndex> if it appear more then 1 time, keeping only one.

I have surrounded the item i want to delete here with ***.. The code i am using worked before, but then i added .Value == "shipments" and now it fail. How can i keep this code and only fix .Value == "shipments" to work?

 class Program
    {
        static void Main(string[] args)
        {
            string renderedOutput =
                    "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                     "<RootDTO xmlns:json='http://james.newtonking.com/projects/json'>" +
                       "<destination>" +
                          "<name>xxx</name>" +
                       "</destination>" +
                       "<orderData>" +
                          "<items json:Array='true'>" +
                                "<shipmentIndex Name=\"items\" >111</shipmentIndex>" +
                                "<barcode>12345</barcode>" +
                          "</items>" +
                           "<items json:Array='true'>" +
                                "<shipmentIndex Name=\"items\">222</shipmentIndex>" +
                                "<barcode>12345</barcode>" +
                          "</items>" +
                           "<items json:Array='true'>" +
                                "<shipmentIndex Name=\"items\">222</shipmentIndex>" +
                                "<barcode>12345</barcode>" +
                          "</items>" +


                          "<misCode>9876543210</misCode>" +
                              "<shipments>" +
                                    "<sourceShipmentId></sourceShipmentId>" +
                                    "<shipm        

Answers


MaxJ February 2016

Not sure I understand the question 100% but here goes:

I am thinking you want to filter the results to only include those elements where the name attribute is equal to 'shipments', although not all of the shipmentIndex elements have a 'Name' attribute so you are probably getting a null reference exception. You need to add a check to ensure that the 'Name' attribute exists.

xml.Element("orderData").Descendants("shipments")
               .SelectMany(s => s.Elements("shipmentIndex")
               .GroupBy(g => g.Attribute("Name") != null && g.Attribute("Name").Value == "shipments")
               .SelectMany(m => m.Skip(1))).Remove();


darkend February 2016

If you want to delete the duplicate from the renderedOutput string:

Match match = Regex.Match(renderedOutput, "<shipmentIndex Name=\"shipments\">([^<]*)</shipmentIndex>");

int index = renderedOutput.IndexOf(match.ToString());
renderedOutput = renderedOutput.Remove(index, match.ToString().Length);

Post Status

Asked in February 2016
Viewed 2,628 times
Voted 13
Answered 2 times

Search




Leave an answer