Solved

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

Posted on 2016-10-10
4
70 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 

Author Comment

by:vcharles
ID: 41839091
Thanks.
0
 
LVL 63

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

749 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