Solved

merge 2 xml documents

Posted on 2011-09-07
8
547 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 74

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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 74

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

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…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This video discusses moving either the default database or any database to a new volume.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

744 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

11 Experts available now in Live!

Get 1:1 Help Now