Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2016-10-10
4
Medium Priority
?
90 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 64

Accepted Solution

by:
Fernando Soto earned 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

604 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