380380
380380

Reputation: 109

adding xml node from a file as a node of another document

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

Answers (1)

bobince
bobince

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

Related Questions