tags:

views:

39

answers:

1

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!

+1  A: 

To copy content from one document to another, use Document.importNode (W3C standard, .NET implementation docs).

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