通过属性从XML中删除重复项

时间:2016-02-08 14:18:59

标签: c# xml

尝试删除<shipmentIndex Name=\"shipments\">whatever...</shipmentIndex> 如果它出现超过1次,只保留一次。

我已经用***包围了我要删除的项目.. 我之前使用的代码工作,但后来我添加了.Value == "shipments" 现在它失败了。 如何保留此代码并仅修复.Value == "shipments"才能正常工作?

 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>" +
                                    "<shipmentIndex shipments=\"shipments\">111</shipmentIndex>" +
                              "</shipments>" +
                              "<shipments>" +
                                    "<sourceShipmentId></sourceShipmentId>" +
                                    "<shipmentIndex Name=\"shipments\">222</shipmentIndex>" +
****
                                    "<shipmentIndex Name=\"shipments\">222</shipmentIndex>" +
****
                              "</shipments>" +
                       "</orderData>" +
                      "</RootDTO>";


            var xml = XElement.Parse(renderedOutput);    

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

2 个答案:

答案 0 :(得分:1)

不确定我100%理解这个问题但是这里有:

我想你想过滤结果,只包括name属性等于&#39;货运的那些元素,尽管不是所有的shippingIndex元素都有一个&#39; Name&#39;属性,所以你可能得到一个空引用异常。您需要添加一项检查,以确保&#39;名称&#39;属性存在。

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();

答案 1 :(得分:0)

如果要从renderedOutput字符串中删除副本:

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

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