删除重复的XML记录

时间:2015-06-19 20:08:26

标签: c# linq xpath

我想删除xml中的重复记录,但到目前为止我无法确定如何执行此操作,这里是xml,您可以看到有4个重复记录。我想删除 itemGrp 节点,因为在另一个 itemGrp 节点中具有相同的 rateClass 元素

<?xml version="1.0" encoding="utf-8" ?>
<Fare_1 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Fare_1_Details xmlns="http://xml.someplace">
    <itemGrp>
      <itemNb>
        <itemNumberDetails>
          <number>01</number>
        </itemNumberDetails>
      </itemNb>
      <fareQualifItem>
        <additionalFareDetails>
          <rateClass>DT21P</rateClass>
        </additionalFareDetails>
        <discountDetails>
          <fareQualifier>725</fareQualifier>
        </discountDetails>
      </fareQualifItem>
    </itemGrp>
    <itemGrp>
      <itemNb>
        <itemNumberDetails>
          <number>02</number>
        </itemNumberDetails>
      </itemNb>
      <fareQualifItem>
        <additionalFareDetails>
          <rateClass>DT21P</rateClass>
        </additionalFareDetails>
        <discountDetails>
          <fareQualifier>725</fareQualifier>
        </discountDetails>
      </fareQualifItem>
    </itemGrp>
    <itemGrp>
      <itemNb>
        <itemNumberDetails>
          <number>03</number>
        </itemNumberDetails>
      </itemNb>
      <fareQualifItem>
        <additionalFareDetails>
          <rateClass>CV76J</rateClass>
        </additionalFareDetails>
        <discountDetails>
          <fareQualifier>352</fareQualifier>
        </discountDetails>
      </fareQualifItem>
    </itemGrp>
  </Fare_1_Details>
  <Fare_1_Details xmlns="http://xml.someplace">
    <itemGrp>
      <itemNb>
        <itemNumberDetails>
          <number>01</number>
        </itemNumberDetails>
      </itemNb>
      <fareQualifItem>
        <additionalFareDetails>
          <rateClass>TH31L</rateClass>
        </additionalFareDetails>
        <discountDetails>
          <fareQualifier>722</fareQualifier>
        </discountDetails>
      </fareQualifItem>
    </itemGrp>
    <itemGrp>
      <itemNb>
        <itemNumberDetails>
          <number>02</number>
        </itemNumberDetails>
      </itemNb>
      <fareQualifItem>
        <additionalFareDetails>
          <rateClass>GK51T</rateClass>
        </additionalFareDetails>
        <discountDetails>
          <fareQualifier>726</fareQualifier>
        </discountDetails>
      </fareQualifItem>
    </itemGrp>
    <itemGrp>
      <itemNb>
        <itemNumberDetails>
          <number>03</number>
        </itemNumberDetails>
      </itemNb>
      <fareQualifItem>
        <additionalFareDetails>
          <rateClass>GK51T</rateClass>
        </additionalFareDetails>
        <discountDetails>
          <fareQualifier>726</fareQualifier>
        </discountDetails>
      </fareQualifItem>
    </itemGrp>
  </Fare_1_Details>
</Fare_1>

到目前为止,我尝试使用XmlDocument:

NameTable nt = new NameTable();
XmlNamespaceManager m = new XmlNamespaceManager(nt);
m.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
m.AddNamespace("a", "http://xml.someplace");
XmlNodeList nodes = xmlTree.SelectNodes("//Fare_1/Fare_1_Details/itemGrp", m);
XmlNode actionNode = xmlTree.SelectSingleNode("//Fare_1");
for (int i = 1; i < nodes.Count; i++)
{
    actionNode.RemoveChild(nodes[i]);
}

1 个答案:

答案 0 :(得分:1)

通过编写此代码,我已经能够为您提供解决方案:

string file = File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + "xml.txt");
            var xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(file);
            var el = xmlDoc.SelectSingleNode("/Fare_1");
            var c = el.ChildNodes;
            List<string> rateClassValues = new List<string>();
            foreach (XmlNode x in c)
            {
               foreach(XmlNode ch in x.ChildNodes)
               {
                   var rateClassValue = ch.FirstChild.NextSibling.FirstChild.FirstChild.InnerText;
                   if (!rateClassValues.Contains(rateClassValue))
                   {
                       // add value to list to keep track of rate class values
                       rateClassValues.Add(rateClassValue);
                   }
                   else if (rateClassValues.Contains(rateClassValue))
                   {
                       // delete duplicate itemGrp from parent
                       XmlNode parent = ch.ParentNode;
                       parent.RemoveChild(ch);
                   }
               }
            }

            xmlDoc.Save(AppDomain.CurrentDomain.BaseDirectory + "xmlNew.txt");

此代码删除任何具有重复rateClass innerText的ItemGrp。运行此代码会得到以下结果:

<?xml version="1.0" encoding="UTF-8"?>
<Fare_1 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <Fare_1_Details xmlns="http://xml.someplace">
      <itemGrp>
         <itemNb>
            <itemNumberDetails>
               <number>01</number>
            </itemNumberDetails>
         </itemNb>
         <fareQualifItem>
            <additionalFareDetails>
               <rateClass>DT21P</rateClass>
            </additionalFareDetails>
            <discountDetails>
               <fareQualifier>725</fareQualifier>
            </discountDetails>
         </fareQualifItem>
      </itemGrp>
      <itemGrp>
         <itemNb>
            <itemNumberDetails>
               <number>03</number>
            </itemNumberDetails>
         </itemNb>
         <fareQualifItem>
            <additionalFareDetails>
               <rateClass>CV76J</rateClass>
            </additionalFareDetails>
            <discountDetails>
               <fareQualifier>352</fareQualifier>
            </discountDetails>
         </fareQualifItem>
      </itemGrp>
   </Fare_1_Details>
   <Fare_1_Details xmlns="http://xml.someplace">
      <itemGrp>
         <itemNb>
            <itemNumberDetails>
               <number>01</number>
            </itemNumberDetails>
         </itemNb>
         <fareQualifItem>
            <additionalFareDetails>
               <rateClass>TH31L</rateClass>
            </additionalFareDetails>
            <discountDetails>
               <fareQualifier>722</fareQualifier>
            </discountDetails>
         </fareQualifItem>
      </itemGrp>
      <itemGrp>
         <itemNb>
            <itemNumberDetails>
               <number>02</number>
            </itemNumberDetails>
         </itemNb>
         <fareQualifItem>
            <additionalFareDetails>
               <rateClass>GK51T</rateClass>
            </additionalFareDetails>
            <discountDetails>
               <fareQualifier>726</fareQualifier>
            </discountDetails>
         </fareQualifItem>
      </itemGrp>
   </Fare_1_Details>
</Fare_1>

我希望我能够击中头部。干杯!