merge 2 xml documents

xmldocument1------ <a><b></b></a>

xmldocument2-------- <c></c>


How to merge above 2 xmls using c#. the expected result should be

<a><b></b></a><c></c>
mkarthik415Asked:
Who is Participating?
 
käµfm³d 👽Connect With a Mentor Commented:
How to merge above 2 xmls using c#. the expected result should be
<a><b></b></a><c></c>
You do realise that that result is actually invalid XML, right? XML does not permit two root nodes in one document.
0
 
Fernando SotoRetiredCommented:
Hi mkarthik415;

If you are using Visual Studio 2008 with SP1 installed then you can use Linq to XML to do what you need as seen in the following code snippet.

XDocument xdoc1 = XDocument.Load("C:/Working Directory/FirstDoc.xml");
XDocument xdoc2 = XDocument.Load("C:/Working Directory/SecondDoc.xml");

xdoc1.Root.Add(xdoc2.Root.Descendants());

Open in new window


Fernando
0
 
Fernando SotoRetiredCommented:
Hi mkarthik415;

Of course if you want to safe the new document to the file system you would then do the following:

xdoc1.Save("File path and name as a string parameter");

Fernando
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Fernando SotoRetiredCommented:
Hi kaufmed;

I assumed that he did not display the root nodes and just the inner nodes. But that may be a wrong assumption.

Fernando
0
 
käµfm³d 👽Commented:
Fernando,

That was directed to the author, not you. Sorry for the confusion  = )


...Like I'd dare question your Linq skillz  8-p
0
 
Fernando SotoRetiredCommented:
Hi kaufmed;

Thanks, no confusion, just wanted to let you and mkarthik415 know how I understood the question.

Thanks
0
 
mkarthik415Author Commented:
"XML does not permit two root nodes in one document" answers my question. Is it possible to have multiple root elements in any  xml.net classes.

Thank You.
0
 
Fernando SotoRetiredCommented:
"XML does not permit two root nodes in one document"
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.