[英]Append an xml document to an xml node in C#?
如何將XML文檔附加到c#中的xml節點?
一個XmlDocument
基本上是一個XmlNode
,所以你可以添加它,就像你會為任何其他做XmlNode
。 但是,不同之處在於此 XmlNode
不屬於目標文檔,因此您需要使用ImportNode方法然后執行追加。
// xImportDoc is the XmlDocument to be imported.
// xTargetNode is the XmlNode into which the import is to be done.
XmlNode xChildNode = xSrcNode.ImportNode(xImportDoc, true);
xTargetNode.AppendChild(xChildNode);
是:
XmlNode imported = targetNode.OwnerDocument.ImportNode(otherDocument.DocumentElement, true);
targetNode.AppendChild(imported);
我認為這會創建一個文檔的克隆。
假設你有以下結構:
以下結構存儲在名為xmlElement的XmlElement中:
</root>
並且以下結構存儲在名為FooNode的XmlNode對象中;
<foo>
<bar>This is a test</bar>
<baz>And this is another test</baz>
</foo>
然后執行以下操作:
XmlNode node = doc.ImportNode(FooNode.SelectSingleNode("foo"), true);
xmlElement.AppendChild(node);
希望它可以幫助某人
也許是這樣的:
XmlNode node = ...... // belongs to targetDoc (XmlDocument)
node.AppendChild(targetDoc.ImportNode(xmlDoc.DocumentElement));
渣
獲得有問題的XML文檔的根節點后,可以將其作為相關節點的子節點附加。 那有意義嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.