2010-08-07 84 views
2

我加載XML文檔的另一個文檔的節點:添加XML節點從文件

XmlDocument xmlDoc = new XmlDocument(); 
xmlDoc.Load("MyFile.xml"); 

,也可以創建一個新的文件:

XmlDocument xmlDocSettings = new XmlDocument(); 
XmlNode xmlDecl = xmlDocSettings.CreateNode(XmlNodeType.XmlDeclaration, "", ""); 
xmlDocSettings.AppendChild(xmlDecl); 
XmlElement root = xmlDocSettings.CreateElement("", "Test", ""); 
root.SetAttribute("TestAttribute", "AttributeValue"); 
xmlDocSettings.AppendChild(root); 

現在我想插入的內容xmlDocxmlDocSettings。我怎樣才能做到這一點?

謝謝!

回答

3

要將內容從一個文檔複製到另一個文檔,請使用Document.importNodeW3C standard,.NET implementation文檔)。

xmlDocSettings.DocumentElement.AppendChild(
    xmlDocSettings.ImportNode(xmlDoc.DocumentElement, true) 
);