Solved

merge 2 xml documents

Posted on 2011-09-07
8
555 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
8 Comments
 
LVL 63

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 63

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 63

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
 
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 63

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 63

Expert Comment

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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

726 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