Help with comparing two xml files and delete all data element in File1 found in File2 using VB.NET

Hi,

I have a list of data elements in File2  I wand to delete in File1, how do I acheive this using VB.NET?  

For example if File2 contains

<Table2>
<NASC></NASC>
<AGD></AGD>
</Table2>

and File1 contains

<Table1>
<ID>1</ID>
<NASC>a</NASC>
<AGD>b</AGD>
<DGG>C</DGG>
<NAS>D</NAS>
</Table1>
<Table1>
<ID>2</ID>
<NASC>a1</NASC>
<AGD>b1</AGD>
<DGG>C1</DGG>
<NAS>D1</NAS>
</Table1>
<Table1>
<ID>3</ID>
<NASC>a2</NASC>
<AGD>b2</AGD>
<DGG>C2</DGG>
<NAS>D2</NAS>
</Table1>

New file created should be

and File1 contains

<Table1>
<ID>1</ID>
<DGG>C</DGG>
<NAS>D</NAS>
</Table1>
<Table1>
<ID>2</ID>
<DGG>C1</DGG>
<NAS>D1</NAS>
</Table1>
<Table1>
<ID>3</ID>
<DGG>C2</DGG>
<NAS>D2</NAS>
</Table1>

Also how do I implement the reverse, for example would like to also add all data elements in File2 to File1. For example if  File1 contains:


<Table1>
<ID>1</ID>
<DGG>C</DGG>
<NAS>D</NAS>
</Table1>
<Table1>
<ID>2</ID>
<DGG>C1</DGG>
<NAS>D1</NAS>
</Table1>
<Table1>
<ID>3</ID>
<DGG>C2</DGG>
<NAS>D2</NAS>
</Table1>

and File2 contains:

<Table2>
<NASC></NASC>
<AGD></AGD>
</Table2>

How do I create the following file:

<Table1>
<ID>1</ID>
<NASC></NASC>
<AGD></AGD>
<DGG>C</DGG>
<NAS>D</NAS>
</Table1>
<Table1>
<ID>2</ID>
<NASC></NASC>
<AGD></AGD>
<DGG>C1</DGG>
<NAS>D1</NAS>
</Table1>
<Table1>
<ID>3</ID>
<NASC></NASC>
<AGD></AGD>
<DGG>C2</DGG>
<NAS>D2</NAS>
</Table1>

Thanks,

Victor
vcharlesAsked:
Who is Participating?

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

x
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.

Fernando SotoRetiredCommented:
Hi Victor;

Try this, it should do what you need for the first question. You can only ask one question per post please open a new question for the second question

'' Load XML document to be modified
Dim xdoc = XDocument.Load("C:\Working Directory\VictorFile1.xml")   
'' Load the XML document with the nodes to be deleted from file1
Dim xdocDel = XDocument.Load("C:\Working Directory\VictorFile2.xml")
                                                                
'' Query file2 to net the node names as string of nodes to be removed from file1                                                                    
Dim tagsToDelete As List(Of String) =                               
    (From n In xdocDel.Descendants("Table2").Elements()             
     Select n.Name.LocalName).ToList()                              

'' Get the list of the nodes to be removed                                                                    
Dim results = (From n In xdoc.Descendants("Table1").Elements()      
               Where tagsToDelete.Contains(n.Name.LocalName)        
               Select n).ToList()                                   
  
'' Remove the nodes from the XDocument                                                                  
results.ForEach(Sub(n) n.Remove())    

'' Save the modified document
xdoc.Save("File path and name goes here") 

Open in new window

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
vcharlesAuthor Commented:
Thank You.
Easwaran ParamasivamCommented:
You need to traverse the source XML and add to the Target XML if the node is not existing already.

This can be achieved using XElement or XMLDocument.

Example to traverse the elements.


Dim xelement As XElement = XElement.Load("..\..\Employees.xml")
Dim employees As IEnumerable(Of XElement) = xelement.Elements()
' Read the entire XML
For Each employee In employees
     
Next employee

Open in new window



Example to add element.


Dim xEle As XElement = XElement.Load("..\..\Employees.xml")
        xEle.Add(New XElement("Employee", _
                              New XElement("EmpId", 5), _
                              New XElement("Phone", "423-555-4224", _
                                           New XAttribute("Type", "Home"))))
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.