從兩個不同的DOM文檔合併XML的最簡單方法是什麼?除了使用Canonical DataReader方法,然後搞亂輸出的DOM之外,還有其他方法嗎?我基本上想要的是AppendChild到XmlElements而沒有得到:The node to be inserted is from a different document context.
這裏是我想要工作的C#代碼,顯然不會(我正在做的是合併兩個文件,它們有一些我感興趣的節點) :如何從不同的DomDocuments合併XML
XmlDocument doc1 = new XmlDocument();
doc1.LoadXml("<a><items><item1/><item2/><item3/></items></a>");
XmlDocument doc2 = new XmlDocument();
doc2.LoadXml("<b><items><item4/><item5/><item6/></items></b>");
XmlNode doc2Node = doc2.SelectSingleNode("/b/items");
XmlNodeList doc1Nodes = doc1.SelectNodes("https://stackoverflow.com/a/items/*");
foreach (XmlNode doc1Node in doc1Nodes)
{
doc2Node.AppendChild(doc1Node);
}
http://php.net/domdocument.importnode – matpie 2009-04-27 15:36:32