Reputation: 109
I load an XML document:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("MyFile.xml");
And also create a new document:
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);
Now I want to insert the content of xmlDoc
to xmlDocSettings
. How can I do that?
Thanks!
Upvotes: 6
Views: 8306
Reputation: 536339
To copy content from one document to another, use Document.importNode
(W3C standard, .NET implementation docs).
xmlDocSettings.DocumentElement.AppendChild(
xmlDocSettings.ImportNode(xmlDoc.DocumentElement, true)
);
Upvotes: 10