• Status: Solved
  • Priority: High
  • Security: Private
  • Views: 50
  • Last Modified:

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,
Output.xml

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.

Thanks
0
Johny Bravo
Asked:
Johny Bravo
  • 6
  • 3
3 Solutions
 
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.
0
 
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.
Thanks
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
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")
                          .Union(doc2.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?
0
 
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)
0
 
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
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
From my first comment:
adding new elements, replacing existing elements.
0
 
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.
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
That should accomplish what was requested
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now