How to add a new root element in C # XmlDocument?

I have an out of my control XmlDocument that has the following structure:

<parent1> ...minor amount of data... </parent1> 

I have another XmlDocument, also out of my control, which has the following structure:

 <parent2> ..very large amount of data... </parent2> 

I need an XmlDocument in the format:

 <parent1> ...minor amount of data... <parent2> ..very large amount of data... </parent2> </parent1> 

I do not want to make a copy of parent2. How can I get the desired structure without copying parent2? I think that means

 oParent1.DocumentElement.AppendChild(oParent1.ImportNode(oParent2.DocumentElement, true)); 

out of the question.

Any good solutions?

+6
optimization c # xmldocument
source share
1 answer

Just remove the DocumentElement from the parent XmlDocument, then add the imported parent 1 node to the XmlDocument (directly - NOT to the DocumentElement) and re-add the remote parent2 node to the imported parent1 node:

 var p1node = oParent2.ImportNode(oParent1.DocumentElement, true); var p2node = oParent2.RemoveChild(oParent2.DocumentElement); oParent2.AppendChild(p1node); p1node.AppendChild(p2node); 
+4
source share

All Articles