Solved

Help with deleting records in File2 when no matching records in both File1 and File2

Posted on 2016-10-10
4
58 Views
Last Modified: 2016-10-12
Hi,

How do I delete records in File2 if the values of two match fields (Receiver, AGD) are not identical? For example when I compare the first two files below, how do I obtain File3.xml which contains data from File2.xml?

File1.xml
 <?xml version="1.0" encoding="utf-8"?>
 <Root>
 <Table1>
   <Link_ID>1</Link_ID>
   <Receiver>USA</Receiver>
   <AGD>XXX</AGD>
   <NSC>FFF</NSC>
   <FCT>UUU</FCT>
 </Table1>
 <Table1>
   <Link_ID>2</Link_ID>
   <Receiver>BEL</Receiver>
   <AGD>XXX</AGD>
   <NSC>GGG</NSC>
   <FCT>HHH</FCT>
 </Table1>
 <Table1>
   <Link_ID>3</Link_ID>
   <Receiver>ESP</Receiver>
   <AGD>XXX</AGD>
   <NSC>EEE</NSC>
   <FCT>PPP</FCT>
 </Table1>
 <Table1>
   <Link_ID>4</Link_ID>
   <Receiver>ROU</Receiver>
   <AGD>VVV</AGD>
   <NSC>EEE</NSC>
   <FCT>PPP</FCT>
 </Table1>
 <Table1>
   <Link_ID>5</Link_ID>
   <Receiver>POL</Receiver>
   <AGD>XXX</AGD>
   <NSC>EEE</NSC>
   <FCT>PPP</FCT>
 </Table1>
</Root>

Open in new window

File2.xml
 <?xml version="1.0" encoding="utf-8"?>
 <Root>
 <Table2>
   <Link_ID>1</Link_ID>
   <Receiver>DEU</Receiver>
   <AGD>LLL</AGD>
   <NSC>FFF</NSC>
   <FCT>UUU</FCT>
 </Table2>
 <Table2>
   <Link_ID>2</Link_ID>
   <Receiver>BEL</Receiver>
   <AGD>XXX</AGD>
   <NSC>GGG</NSC>
   <FCT>HHH</FCT>
 </Table2>
 <Table2>
   <Link_ID>3</Link_ID>
   <Receiver>ESP</Receiver>
   <AGD>XXX</AGD>
   <NSC>EEE</NSC>
   <FCT>PPP</FCT>
 </Table2>
</Root>

Open in new window

File3.xml
 <?xml version="1.0" encoding="utf-8"?>
 <Root>
 <Table2>
   <Link_ID>1</Link_ID>
   <Receiver>DEU</Receiver>
   <AGD>LLL</AGD>
   <NSC>FFF</NSC>
   <FCT>UUU</FCT>
 </Table2>
</Root>

Open in new window

Thanks,

Victor
0
Comment
Question by:vcharles
  • 2
4 Comments
 

Author Comment

by:vcharles
ID: 41839091
Thanks.
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 41841238
Hi Victor;

This should do what you are looking for.
'' Load XML Documents into memory                                                                                                        
Dim file1 = XDocument.Load("File1.XML")                                                                                                 
Dim file2 = XDocument.Load("File2.XML")                                                                                                 
                                                                                                                                        
'' Go through the nodes and transform into the two value you will be searching for in the other XML document                            
Dim file1List = (From n1 In file1.Root.Elements                                                                                         
                 Select New With {                                                                                                      
                     .Receiver = n1.Element("Receiver").Value,                                                                          
                     .AGD = n1.Element("AGD").Value                                                                                     
                }).ToList()                                                                                                             
                                                                                                                                        
'' Find all nodes in document 2 that do not have the same values in document 1                                                          
Dim addToFile3 = (From n2 As XElement In file2.Root.Elements                                                                            
                  Where Not file1List.Any(Function(r) r.Receiver = n2.Element("Receiver").Value AndAlso r.AGD = n2.Element("AGD").Value)
                  Select n2                                                                                                             
                 ).ToList()                                                                                                             
                                                                                                                                        
If addToFile3.Count > 0 Then                                                                                                            
    '' Create new XDocument for the results                                                                                             
    Dim file3 = New XDocument(New XDeclaration(file1.Declaration))                                                                      
    file3.Add(New XElement("Root"))                                                                                                     
                                                                                                                                        
    '' Add all the XElements from the addToFile3 list                                                                                   
    For Each n As XElement In addToFile3                                                                                                
        file3.Root.Add(n)                                                                                                               
    Next                                                                                                                                
    
    '' Save the new document to the file system                                                                                                                                        
    file3.Save("file3.xml")                                                                                                                                                                                                                 
End If

Open in new window

0
 

Author Comment

by:vcharles
ID: 41841383
Hi Fernando,

It works.

Thank You.

Victor
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question