删除重复的 XML 记录



我想删除 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]);
}

我已经能够通过编写以下代码为您提出解决方案:

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 内部文本的任何 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>

我希望我能够一针见血。干杯!

相关内容

  • 没有找到相关文章

最新更新