从两个xmlnodelist构建一个新的XML

本文关键字:一个 XML xmlnodelist 两个 构建 | 更新日期: 2023-09-27 18:29:05

我们如何通过从两个xmlnodelist,中获取值来构建xml

Ex--->Xmlnodelist1:

<D>
  <F a="1" b="2" c="3">
     <B d="4" e="5" f="6" g="7"/>
     <B d="5" e="5" f="11" g="7"/>
     <B d="6" e="5" f="23" g="8"/>
     <B d="7" e="5" f="45" g="9"/>
   </F>
</D>  

Xmlnodelist2:

<Z aa="1">
       <s e="4" ee="5" ae="6"/>
       <s e="5" ee="55" ae="6"/>
       <s e="6" ee="555" ae="6"/>
       <s e="7" ee="5555" ae="6"/>
    </Z>

这里比较xmlnodelist1中的"d"值和xmlnodelist2中的"e"值,得到"g"、"f"answers"ae"的值,并构建一个类似xml的->

 <Root>
         <T g="7" f="45" ar="6">
         <T g="7" f="45" ar="6">
         <T g="7" f="45" ar="6">
         <T g="7" f="45" ar="6">
    </Root> 

这只是一个例子。请回答。感谢

从两个xmlnodelist构建一个新的XML

您可以使用Linq来Xml。下面的示例没有提供您的示例的确切结果,因为我不完全理解这两个列表之间的关系,但这只是一个开始:

        XElement xml1 =
            XElement.Parse("<D>" +
                            "  <F a='"1'" b='"2'" c='"3'">" +
                            "     <B d='"4'" e='"5'" f='"6'" g='"7'"/>" +
                            "     <B d='"5'" e='"5'" f='"11'" g='"7'"/>" +
                            "     <B d='"6'" e='"5'" f='"23'" g='"8'"/>" +
                            "     <B d='"7'" e='"5'" f='"45'" g='"9'"/>" +
                            "  </F>" +
                            "</D>");
        XElement xml2 =
            XElement.Parse("<Z aa='"1'">" +
                            "  <s e='"4'" ee='"5'" ae='"6'"/>" +
                            "  <s e='"5'" ee='"55'" ae='"6'"/>" +
                            "  <s e='"6'" ee='"555'" ae='"6'"/>" +
                            "  <s e='"7'" ee='"5555'" ae='"6'"/>" +
                            "</Z>");
        // I join list1 and list2 with attribute d and e
        IEnumerable<XElement> result = from list1 in xml1.Descendants("B")
                                       join list2 in xml2.Descendants("s")
                                       on list1.Attribute("d").Value equals list2.Attribute("e").Value
                                       select new XElement("T", new XAttribute("g", list1.Attribute("g").Value),
                                           new XAttribute("f", list1.Attribute("f").Value),
                                           new XAttribute("ar", list2.Attribute("ae").Value));
        var test = new XElement("Root", result);

结果是:

<Root>
  <T g="7" f="6" ar="6" />
  <T g="7" f="11" ar="6" />
  <T g="8" f="23" ar="6" />
  <T g="9" f="45" ar="6" />
</Root>