Solved

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

Posted on 2016-10-10
4
41 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now