Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to merge two XmlDocuments in C#

Tags:

c#

.net

xml

I want to merge two XmlDocuments by inserting a second XML doc to the end of an existing Xmldocument in C#. How is this done?

like image 757
Harold Sota Avatar asked Aug 09 '10 12:08

Harold Sota


People also ask

Can we use if condition in XML?

You can't, as such: XML isn't a programming language. But you can have conditional criteria in a Schema, DTD, or a processor, and some DTDs provide attributes for conditional processing. If you need to make an element optional, based on some internal or external criteria, you can do so in a Schema.

How do I merge XML files in Python?

Code Explanation First, we have imported a required module, And to merge two XML files in python, we have imported ElementTree Module. The ElementTree. getroot() method returns a root element of each document. Finally, to add the element of one tree to the other, we will make use of the element.


4 Answers

Something like this:

foreach (XmlNode node in documentB.DocumentElement.ChildNodes)
{
    XmlNode imported = documentA.ImportNode(node, true);
    documentA.DocumentElement.AppendChild(imported);
}

Note that this ignores the document element itself of document B - so if that has a different element name, or attributes you want to copy over, you'll need to work out exactly what you want to do.

EDIT: If, as per your comment, you want to embed the whole of document B within document A, that's relatively easy:

XmlNode importedDocument = documentA.ImportNode(documentB.DocumentElement, true);
documentA.DocumentElement.AppendChild(importedDocument);

This will still ignore things like the XML declaration of document B if there is one - I don't know what would happen if you tried to import the document itself as a node of a different document, and it included an XML declaration... but I suspect this will do what you want.

like image 189
Jon Skeet Avatar answered Oct 01 '22 17:10

Jon Skeet


Inserting an entire XML document at the end of another XML document is actually guaranteed to produce invalid XML. XML requires that there be one, and only one "document" element. So, assuming that your files were as follows:

A.xml

<document>
   <element>value1</element>
   <element>value2</element>
</document>

B.xml

<document>
   <element>value3</element>
   <element>value4</element>
</document>

The resultant document by just appending one at the end of the other:

<document>
   <element>value1</element>
   <element>value2</element>
</document>
<document>
   <element>value3</element>
   <element>value4</element>
</document>

Is invalid XML.

Assuming, instead, that the two documents share a common document element, and you want to insert the children of the document element from B into A's document element, you could use the following:

var docA = new XmlDocument();
var docB = new XmlDocument();

foreach (var childEl in docB.DocumentElement.ChildNodes) {
    var newNode = docA.ImportNode(childEl, true);
    docA.DocumentElement.AppendChild(newNode);
}

This will produce the following document given my examples above:

<document>
   <element>value1</element>
   <element>value2</element>
   <element>value3</element>
   <element>value4</element>
</document>
like image 45
Ryan Brunner Avatar answered Oct 03 '22 17:10

Ryan Brunner


This is the fastest cleanest way to merge xml documents.

XElement xFileRoot = XElement.Load(file1.xml);
XElement xFileChild = XElement.Load(file2.xml);
xFileRoot.Add(xFileChild);
xFileRoot.Save(file1.xml);
like image 42
Rudy Hinojosa Avatar answered Oct 05 '22 17:10

Rudy Hinojosa


Bad news. As long as the xml documents can have only one root element you cannot just put content of one document at the end of the second. Maybe this is what you are looking for? It shows how easily you can merge xml files using Linq-to-XML

Alternatively if you are using XmlDocuments you can try make it like this:

XmlDocument documentA;
XmlDocument documentB;

foreach(var childNode in documentA.DocumentElement.ChildNodes)
   documentB.DocumentElement.AppendChild(childNode);
like image 24
Łukasz W. Avatar answered Oct 05 '22 17:10

Łukasz W.