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
Solved

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

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

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

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

856 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