Solved

merge 2 xml documents

Posted on 2011-09-07
8
552 Views
Last Modified: 2012-05-12
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>
0
Comment
Question by:mkarthik415
  • 5
  • 2
8 Comments
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 36498406
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
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 36498433
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
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 36508037
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
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 36508081
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36510249
Fernando,

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


...Like I'd dare question your Linq skillz  8-p
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 36510649
Hi kaufmed;

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

Thanks
0
 

Author Comment

by:mkarthik415
ID: 36511372
"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
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 36511494
"XML does not permit two root nodes in one document"
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now