![](/img/trans.png)
[英]How do you use linq to xml to find matching nodes in two different xml files
[英]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 >
我想將所有節點files
和scrp
提取到一個新的XML
文件。 我做了如下代碼,但它只保存files
節點而不是節點files
和scrp
。 我可以請你幫忙嗎?
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.