troubleshooting Question

Remove a sub element from XML file using C#

Avatar of Saxitalis
Saxitalis asked on
.NET ProgrammingC#
11 Comments1 Solution2828 ViewsLast Modified:
Hello Experts,

I have two XML files that are identical except for one or more elements. Is there a way to compare these two XML files and remove a particular sub element from every top level element from file2 that is not present in file1?

For Example:
XML file1 looks like this:
   <1stLevel_element>
- <2ndLevel_ element >
  <item_number>1</item_number>
  <item2>IAA</ item2>
  <item3>6</ item3>
  <item4>P</ item4>
  <item5>8</ item5>
  <item6>44.327</ item6>
</2ndLevel_element>
- <2ndLevel_element>
  <item_number>2</item_number>
  < item2>IAA</ item2>
  < item3>6</ item3>
  < item4>P</ item4>
  < item5>44</ item5>
  < item6>0.09</ item6>
</2ndLevel_element>
</1stLevel_ element >

XML file2 looks like this:
   <1stLevel_ element >
- <2ndLevel_ element >
  <item_number>1</item_number>
  <item2>IAA</ item2>
  <item3>6</ item3>
  <item4>P</ item4>
  <item5>8</ item5>
  <item6>44.327</ item6>
</2ndLevel_element>
- <2ndLevel_element>
  <item_number>2</item_number>
  < item2>IAA</ item2>
  < item3>6</ item3>
  < item4>P</ item4>
  < item5>44</ item5>
  < item6>0.09</ item6>
</2ndLevel_element>
- <2ndLevel_element>
  <item_number>64</item_number>
  < item2>IAA</ item2>
  < item3>6</ item3>
  < item4>P</ item4>
  < item5>500</ item5>
  < item6>0.09</ item6>
</2ndLevel_element>
</1stLevel_ element >

Notice that there are three 2ndLevel elements in XML file2 and only two 2ndLevel elements in XML file#1 and the first two 2ndLevel elements in both files are identical.

 Is there a way to compare the two files and then remove the item_number sub elements from any 2ndLevel elements in XML file 2 that do not have coorosponding 2ndLevel elements in XML file1??

In other words I would like to get XML file2 to look like this:
   <1stLevel_ element >
- <2ndLevel_ element >
  <item_number>1</item_number>
  <item2>IAA</ item2>
  <item3>6</ item3>
  <item4>P</ item4>
  <item5>8</ item5>
  <item6>44.327</ item6>
</2ndLevel_element>
- <2ndLevel_element>
  <item_number>2</item_number>
  < item2>IAA</ item2>
  < item3>6</ item3>
  < item4>P</ item4>
  < item5>44</ item5>
  < item6>0.09</ item6>
</2ndLevel_element>
- <2ndLevel_element>
   < item2>IAA</ item2>
  < item3>6</ item3>
  < item4>P</ item4>
  < item5>500</ item5>
  < item6>0.09</ item6>
</2ndLevel_element>
</1stLevel_element >

 notice - the item_number markup (<item_number>64</item_number> ) was removed from file2 but the rest of the subitems remain.

Does anyone know how to do this??

Thanks!
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 11 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 11 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros