Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Help with creating new xml file when comparing two xml files with two or more identical values in a data element using VB.NET

Posted on 2016-11-30
18
Medium Priority
?
54 Views
Last Modified: 2016-11-30
Hi,

How do you create a new xml file (i.e. File2.xml) for all records in File1.xml where two or more records have the same values in the NSN field?  For example if:

 File1.xml:

  <Root>
  <Table1>
  <ID>1</ID>
  <Receiver>BEL</Receiver>
  <Donor>USA</Donor>
  <NSN>111</NSN>
  </Table1>
  <Table1>
  <ID>2</ID>
  <Receiver>FRA</Receiver>
  <Donor>DNK</Donor>
  <NSN>112</NSN>
  </Table1>
  <Table1>
  <ID>3</ID>
  <Receiver>ITA</Receiver>
  <Donor>GBR</Donor>
  <NSN>113</NSN>
  </Table1>
 <Table1>
  <ID>4</ID>
  <Receiver>POL</Receiver>
  <Donor>HUN</Donor>
  <NSN>112</NSN>
  </Table1>
  <ID>6</ID>
  <Receiver>ESP</Receiver>
  <Donor>LUX</Donor>
  <NSN>113</NSN>
  </Table1>
  </Root>

How do I create File2.xml sorted by NSN?

Fie2.xml
 
<Root>
  <Table1>
  <ID>2</ID>
  <Receiver>FRA</Receiver>
  <Donor>DNK</Donor>
  <NSN>112</NSN>
  </Table1>
 <Table1>
  <ID>4</ID>
  <Receiver>POL</Receiver>
  <Donor>HUN</Donor>
  <NSN>112</NSN>
  </Table1>
  <Table1>
  <ID>3</ID>
  <Receiver>ITA</Receiver>
  <Donor>GBR</Donor>
  <NSN>113</NSN>
  </Table1>
  <Table1>
  <ID>6</ID>
  <Receiver>ESP</Receiver>
  <Donor>LUX</Donor>
  <NSN>113</NSN>
  </Table1>
  </Root>

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
  • 8
  • 8
  • 2
18 Comments
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 41907476
Hi Victor;

I ordered the nodes in numerical order if NSN can have alphabetical characters then you will need to remove Integer.Parse( ) leaving node.Element("NSN").Value in the query.
'' Load XML file1
Dim XDocF1 = XDocument.Load("C:\Working Directory\File1Unsorted.xml")
'' Order the nodes                                                                     
Dim results = (From node In XDocF1.Descendants("Table1")             
               Order By Integer.Parse(node.Element("NSN").Value)     
               Select node).ToList()                                       
'' Create new XML document from XDocF1			                                                               
Dim XDocF2 = XDocF1.Document()        
'' Replace all nodes with the ordered set of nodes                               
XDocF2.Root.ReplaceAll(results)                   
'' Save the new Document                   
XDocF2.Save("Give file name and path for new document")		

Open in new window

0
 

Author Comment

by:vcharles
ID: 41907505
Thank You, I will try it and get back to you.
0
 
LVL 13

Expert Comment

by:Jeff Darling
ID: 41907528
I noticed that the first XML file is invalid.  There is a mismatched Table1 Tag.
0
Technology Partners: 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!

 

Author Comment

by:vcharles
ID: 41907716
Hi Fernando,

I'm trying the code below but it's not working, the NSN field is separated by dashes (i.e. xxx-xxx-xx), which might be the issue. The file is returning records that don't have two or more identical NSN values.

   'Retreive two or more records with identical NSN from file2.xml

        '' Load XML file2
        Dim XDocF1 = XDocument.Load(Application.StartupPath + "\file2.xml")
        '' Order the nodes                                                                    
        Dim results = (From node In XDocF1.Descendants("Table1")
                       Order By (node.Element("NSN").Value)
                       Select node).ToList()
        '' Create new XML document from XDocF1                                                                                 
        Dim XDocF2 = XDocF1.Document()
        '' Replace all nodes with the ordered set of nodes                              
        XDocF2.Root.ReplaceAll(results)
        '' Save the new Document                  
        XDocF2.Save(Application.StartupPath + "\file3.xml")


Thanks,

Victor
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 41907751
Can you please post a more correct XML file so that I can test with.
0
 

Author Comment

by:vcharles
ID: 41907764
Hi,

Below is another version of the xml files.

File1.xml:

   <Root>
   <Table1>
   <ID>1</ID>
   <Receiver>BEL</Receiver>
   <Donor>USA</Donor>
   <NSN>111-04-11</NSN>
   </Table1>
   <Table1>
   <ID>2</ID>
   <Receiver>FRA</Receiver>
   <Donor>DNK</Donor>
   <NSN>112-05-11</NSN>
   </Table1>
   <Table1>
   <ID>3</ID>
   <Receiver>ITA</Receiver>
   <Donor>GBR</Donor>
   <NSN>113-09-99</NSN>
   </Table1>
  <Table1>
   <ID>4</ID>
   <Receiver>POL</Receiver>
   <Donor>HUN</Donor>
   <NSN>112-05-11</NSN>
   </Table1>
  <Table1>
   <ID>6</ID>
   <Receiver>ESP</Receiver>
   <Donor>LUX</Donor>
   <NSN>113-08-55</NSN>
   </Table1>
  <Table1>
   <ID>7</ID>
   <Receiver>GBR</Receiver>
   <Donor>LUX</Donor>
   <NSN>113-08-55</NSN>
   </Table1>
  <Table1>
   <ID>8</ID>
   <Receiver>ESP</Receiver>
   <Donor>LUX</Donor>
   <NSN>113-00-55</NSN>
   </Table1>
   </Root>

 How do I create File2.xml sorted by NSN?

 Fie2.xml
 
<Root>
     <Table1>
   <ID>2</ID>
   <Receiver>FRA</Receiver>
   <Donor>DNK</Donor>
   <NSN>112-05-11</NSN>
   </Table1>
   <Table1>
   <ID>4</ID>
   <Receiver>POL</Receiver>
   <Donor>HUN</Donor>
   <NSN>112-05-11</NSN>
   </Table1>
  <Table1>
   <ID>6</ID>
   <Receiver>ESP</Receiver>
   <Donor>LUX</Donor>
   <NSN>113-08-55</NSN>
   </Table1>
  <Table1>
   <ID>7</ID>
   <Receiver>GBR</Receiver>
   <Donor>LUX</Donor>
   <NSN>113-08-55</NSN>
   </Table1>
   </Root>

Only records with two or more records with same NSN values are included in File2.

 Thanks,

 Victor
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 41907786
OK Victor, then try it like this.
'' Load XML file1                                                                                          
Dim XDocF1 = XDocument.Load("C:\Working Directory\File1Unsorted-2.xml")                                    
'' Order the nodes                                                                                         
Dim results = (From node In XDocF1.Descendants("Table1")                                                   
               Group node By node.Element("NSN").Value.Trim() Into nsnGrouping = Group                     
               Where nsnGrouping.Count > 1                                                                       
               Select nsnGrouping.First()).ToList()                                                        
'' Create new XML document from XDocF1			                                                               
Dim XDocF2 = XDocF1.Document()                                                                             
'' Replace all nodes with the ordered set of nodes                                                         
XDocF2.Root.ReplaceAll(results)                                                                            
'' Save the new Document                                                                                   
XDocF2.Save("Give file name and path for new document")

Open in new window

0
 

Author Comment

by:vcharles
ID: 41907803
Hi Fernando,

Thanks for the code, unfortunately records without duplicate NSNs values still show up on File3.xml.

Thanks,

Victor
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 41907808
Hi Victor;

There is only one XML file it processes and the results are stored in a new XML file called XDocF2. Is this what you are calling File3.xml? I tested with the last XML file you posted and it seem to work giving the results as the results you are looking for. You will need to post the XML that this is giving you issues with so I can see what is going on.
0
 
LVL 13

Assisted Solution

by:Jeff Darling
Jeff Darling earned 1000 total points
ID: 41907810
Fernandos code worked when I tried it, except for the select needs to be slightly different.  remove First()

like this.

        '' Load XML file1                                                                                          
        Dim XDocF1 = XDocument.Load("C:\Users\jdarling\ee\20161130\01\file1.xml")
        '' Order the nodes                                                                                         
        Dim results = (From node In XDocF1.Descendants("Table1")
                       Group node By node.Element("NSN").Value.Trim() Into nsnGrouping = Group
                       Where nsnGrouping.Count > 1
                       Select nsnGrouping).ToList()
        '' Create new XML document from XDocF1			                                                               
        Dim XDocF2 = XDocF1.Document()
        '' Replace all nodes with the ordered set of nodes                                                         
        XDocF2.Root.ReplaceAll(results)
        '' Save the new Document                                                                                   
        XDocF2.Save("C:\Users\jdarling\ee\20161130\01\file3.xml")

Open in new window

0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 41907819
@Jeff, from the example Victor posted he only wants one of the two or more to show up in the resulting XML. Removing First() will load all the nodes from each group.
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 41907831
Wow I don't know how I missed that, Thanks. But he also seems to have them Order'ed by.
0
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 1000 total points
ID: 41907833
Victor, update on query.
Dim results = (From node In XDocF1.Descendants("Table1")
               Group node By node.Element("NSN").Value.Trim() Into nsnGrouping = Group
               Where nsnGrouping.Count > 1
               Select nsnGrouping.OrderBy(Function(o) o.Element("NSN").Value.Trim())).ToList()

Open in new window

1
 

Author Comment

by:vcharles
ID: 41907865
Hi Fernando,

I'm used less records to get a better idea of what's going on and  noticed records that have duplicates are listed in file3, but only one record for each. I need to list all the records that have duplicates not only one of each.

Thanks,

Victor
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 41907871
Victor please see my last post.
0
 

Author Comment

by:vcharles
ID: 41907872
Hi,

Just noticed the other messages posted before my message, will get back to you.

Thanks,

Victor
0
 

Author Comment

by:vcharles
ID: 41907881
Hi,

It works!

Thank You both.

Victor
0
 

Author Closing Comment

by:vcharles
ID: 41907882
Thank You.
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

661 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