Merge Xml files in c#

Hi Experts,

I need to merge two xml files, having lack of experience with xml I am struggling here.

I have two xml files. One is original file and second xml file contains new updates to be made in original xml.

This is the original file. file1.xml

Now I have a new xml which contains new update to be done in first xml, please check file2.xml

Now I want to update first xml with the changes available in second xml, so the final xml should look like,

So basically my second xml contains only the changes to be added/ updated in original xml. I believe this should be easy for an expert. (By mistake I types hard earlier :)
Your help here is really appreciated.

Johny BravoAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
>>I believe this is tough for an expert.
Why is that tough?

Read first XML file into your C# app.
Read second XML file, whilst reading modify the in memory XML from the first file, adding new elements, replacing existing elements.
Overwrite the contents of the first XML file with the in memory data.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Johny BravoAuthor Commented:
Hi AndyAinscow

Thanks for the links. I edited question, I didn't mean hard :)

Reading and writing is easy when I know where to writein xml.
But here I want to check new xml, check the nodes, find the same structure in first xml andthen add/ update.
If you can provide me a sample code that would be much better to understand.
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Johny BravoAuthor Commented:
var doc = XDocument.Load(path);
                var doc2 = XDocument.Load("C:/Users/file2.xml");
                var children = doc2.Root.Element("SPaper");
                var parentNode = doc.Root.Element("SPaper");
               // parentNode.ReplaceWith(children);

var combinedUnique = doc.Descendants("SPaper")

Open in new window

(combinedUnique.ToList())[0] //give me xml in 1st file
(combinedUnique.ToList())[1] //give me xml in 2nd file

How to write merged result in 1st xml?
AndyAinscowFreelance programmer / ConsultantCommented:
Your combinedUnique should be an XMLDocument - just save it to the file file name and path to overwrite the first file.  (See my earlier link re writing)
Johny BravoAuthor Commented:
But this will create file only with merged xml, I need to replace first file content with updates in new file. It will not give me the desired o/p which I have shared in my final xml
AndyAinscowFreelance programmer / ConsultantCommented:
From my first comment:
adding new elements, replacing existing elements.
AndyAinscowFreelance programmer / ConsultantCommented:
ps. If the ordering of elements in the end file is not important you can also do the following:
Loop through the nodes in the first document.  If there is a node with the same name in the second document then delete this node (from the first document!).  Then perform your merge.
AndyAinscowFreelance programmer / ConsultantCommented:
That should accomplish what was requested
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.