将相同类型的节点合并到单个节点

时间:2014-01-31 19:33:28

标签: c#

我正在尝试将同一类型的2 Nodes合并为一个Node

所以让这两个Nodes

<Clubs>
  <SPE>Accepted</Community>
  <SCU>Accepted</SCU>
</Clubs>

和这个

<Clubs>
  <BUS>Declined</BUS>
</Clubs>

它会变成这样

<Clubs>
  <SPE>Accepted</SPE>
  <SCU>Accepted</SCU>
  <BUS>Declined</BUS>
</Clubs>

我怎么能实现这样的目标?

1 个答案:

答案 0 :(得分:2)

这可能对您有所帮助

           XmlDocument myDocument = new XmlDocument();
           myDocument.Load(XMLFile);
           var NodeToadd = myDocument.ChildNodes.OfType<XmlElement>().Where(nodeVariant => nodeVariant.Name == "Clubs").SelectMany(o => o.ChildNodes.OfType<XmlElement>()).ToList();
           var nodeToDelete = myDocument.ChildNodes.OfType<XmlElement>().Where(nodeVariant => nodeVariant.Name == "Clubs");
           foreach (var m in nodeToDelete)
           {
               myDocument.RemoveChild(m);
           }
             XmlNode newNode = myDocument.CreateElement("Clubs");
            foreach(var m in NodeToadd)
            {
            newNode.AppendChild(m);
            }
            myDocument.AppendChild(newNode);
            myDocument.Save(XMLFile);