簡體   English   中英

如何提取XML文件的兩個不同節點

[英]How to extract two different nodes of an XML file

我有以下XMl文件,我想提取它的一些節點。 我想提取兩組節點。

<root >
  <comment>
     // something here
  </comment>
  <define>
     // something here
  </define>
  <scrp>
     // something here
  </scrp>
  <files >
  <file id ="1" Name="S1">
    <file id ="2" Name="S11">
      <file id ="3" Name="S111" />
      <file id ="4" Name="S112" />
        <file id ="5" Name="S1121" />
    </file >
    <file id ="6" Name="S12" />
  </file >
</files>
</root >

我想將所有節點filesscrp提取到一個新的XML文件。 我做了如下代碼,但它只保存files節點而不是節點filesscrp 我可以請你幫忙嗎?

var doc = XDocument.Load(xmlfile);
XElement files = doc.Descendants("files").FirstOrDefault();
XElement root = doc.Element("root");
doc.Element("root").ReplaceWith(new XElement("root", new object[] { pack.Attributes(), files }));
doc.Root.ReplaceNodes(new XElement("files", doc.Descendants("file")));

如果需要用特定節點替換根節點的全部內容,則doc.Root上的ReplaceAll應該起作用。 這是你想要實現的目標嗎?

        var doc = XDocument.Load(xmlfile);
        XElement files = doc.Descendants("files").FirstOrDefault();
        XElement scrp = doc.Descendants("scrp").FirstOrDefault();
        doc.Root.ReplaceAll(scrp, files); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM