• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 570
  • Last Modified:

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>
0
mkarthik415
Asked:
mkarthik415
  • 5
  • 2
1 Solution
 
Fernando SotoCommented:
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 SotoCommented:
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
 
käµfm³d 👽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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Fernando SotoCommented:
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 SotoCommented:
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 SotoCommented:
"XML does not permit two root nodes in one document"
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now