简体   繁体   中英

Compare XML files to determine differences

I've been looking around, and though there are several options, none of them really fit my current problem.

I get two XML files, File1 and File2 that have "User" nodes with childnodes detailing things like name, age, address, etc. From there I need to create a third XML File3 that exclusively contains the fields that have changed between the two while keeping the "name" node to identify that the change was done with that user.

So if I get:

File 1:
<users>
 <user>
  <name>Marco</name>
  <height>1,76</height>
  <address>C:/ Far-away 34</address>
  <mail>marco@marco</mail>
 </user>
</users>

File 2:
<users>
 <user>
  <name>Marco</name>
  <height>1,80</height>
  <address>C:/ Far-away 34</address>
  <mail></mail>
 </user>
</users>

It should spit out an XML with:

File 3:
<users>
 <user>
  <name>Marco</name>
  <height>1,80</height>
  <mail></mail>
 </user>
</users>

With one of my main complications being that I am not told which nodes will be present (or whether or not they have childnodes) other than "name".

EDIT: Users only show up once, nodes can be in any order, number of nodes isn't fixed.

You can use XElement class .

Deserialize your XMLs into XElement objects and then you can compare childnodes one by one.

If you know all possible nodes that might appear in this XMLs you can deserialize this XMLs directly into C# objects. But with XElement class you can implement more generic solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM