Solved

Help with comparing xml files

Posted on 2014-11-24
2
61 Views
Last Modified: 2015-01-23
Hi,

How do you modify the code below to compare two xml files based on two data elements instead of only Identical Link_ID and also for Link_ID in LinkFinalA.xml not included in LinkFinal.xml? For example I would like to search records for identical Link_ID and Identical AGD data elements and also for additional records (Link_ID) not included in LinkFinal.xml.

part A:

            ' Get a list of all the Nodes that are in fil2 but not in file1 
            Dim notInFirst As List(Of XElement) = (From id In xdoc2.Root.Descendants("Link_ID")
                                                   Where Not xdoc1.Root.Descendants("Link_ID").Any(Function(i) i.Value = id.Value)
                                                   Select id).ToList()

Open in new window


Complete code:

            ' First XML fie
            Dim xdoc1 As XDocument = XDocument.Load(Application.StartupPath + "\Data\LinkFiles\LinkFinal.xml")
            ' Second XML file
            Dim xdoc2 As XDocument = XDocument.Load(Application.StartupPath + "\Data\LinkFiles\LinkFinalA.xml")
            ' The new XML file
            Dim xdoc3 As New XDocument(New XElement("Root"))
            ' Get a list of all the main nodes
            Dim rowsF1 = From t In xdoc1.Root.Elements("Row")
                         Select t
            ' Go through all the Table1 nodes from file1
            For Each t As XElement In rowsF1
                ' Get its ID node
                Dim id1 As XElement = t.Element("Link_ID")
                If id1 IsNot Nothing Then
                    ' Find the same node in XML file2
                    Dim id2 As XElement = xdoc2.Root.Descendants("Link_ID").Where(Function(i) i.Value = id1).FirstOrDefault()
                    ' Check to see if file2 has this ID 
                    If id2 IsNot Nothing Then
                        ' Get all the individual values from both nodes
                        Dim cal1 As String = id1.Parent.Element("Cal").Value
                        Dim rec1 As String = id1.Parent.Element("Rec").Value
                        
                        Dim cal2 As String = id2.Parent.Element("Cal").Value
                        Dim rec2 As String = id2.Parent.Element("Rec").Value
                        

                        ' Compare all like values for equality
                        If cal1.Equals(cal2) AndAlso rec1.Equals(rec2) AndAlso rec1.Equals(rec2) Then
                            ' Nothing needs to be done because the two nodes are the same
                        Else
                            ' The node from file1 does NOT match the node from file2 add it to the new document
                            xdoc3.Root.Add(id2.Parent)
                        End If
                    Else
                        ' The node in file1 was not found in file2 add this to the new XML file
                        xdoc3.Root.Add(id1.Parent)
                    End If
                End If
            Next

            ' Get a list of all the Nodes that are in fil2 but not in file1 
            Dim notInFirst As List(Of XElement) = (From id In xdoc2.Root.Descendants("Link_ID")
                                                   Where Not xdoc1.Root.Descendants("Link_ID").Any(Function(i) i.Value = id.Value)
                                                   Select id).ToList()

            ' Add all the nodes in the list to the new XML file
            If notInFirst.Count() > 0 Then
                For Each node As XElement In notInFirst
                    xdoc3.Root.Add(node.Parent)
                Next
            End If

            ' Save the document to the file system, change the path location
            xdoc3.Save(Application.StartupPath + "\Data\LinkFiles\DiffFile.xml")

Open in new window


Thanks,

victor
0
Comment
Question by:vcharles
2 Comments
 
LVL 9

Accepted Solution

by:
teebon earned 500 total points
ID: 40463864
Hi Victor,

Try the following:

    ' First XML fie
            Dim xdoc1 As XDocument = XDocument.Load(Application.StartupPath + "\Data\LinkFiles\LinkFinal.xml")
            ' Second XML file
            Dim xdoc2 As XDocument = XDocument.Load(Application.StartupPath + "\Data\LinkFiles\LinkFinalA.xml")
            ' The new XML file
            Dim xdoc3 As New XDocument(New XElement("Root"))
            ' Get a list of all the main nodes
            Dim rowsF1 = From t In xdoc1.Root.Elements("Row")
                         Select t
            ' Go through all the Table1 nodes from file1
            For Each t As XElement In rowsF1
                ' Get its ID node
                Dim id1 As XElement = t.Element("Link_ID")
				Dim agd1 As XElement = t.Element("AGD")
						
				If id1 IsNot Nothing Then
                    ' Find the same node in XML file2
                    Dim id2 As XElement = xdoc2.Root.Descendants("Link_ID").Where(Function(i) i.Value = id1).FirstOrDefault()
        
        			' Check to see if file2 has this ID 
                    If id2 IsNot Nothing AND id2.Parent.Element("AGD").Value = agd1.Value Then
                        ' Get all the individual values from both nodes
                        Dim cal1 As String = id1.Parent.Element("Cal").Value
                        Dim rec1 As String = id1.Parent.Element("Rec").Value
                        
                        Dim cal2 As String = id2.Parent.Element("Cal").Value
                        Dim rec2 As String = id2.Parent.Element("Rec").Value
                        

                        ' Compare all like values for equality
                        If cal1.Equals(cal2) AndAlso rec1.Equals(rec2) AndAlso rec1.Equals(rec2) Then
                            ' Nothing needs to be done because the two nodes are the same
                        Else
                            ' The node from file1 does NOT match the node from file2 add it to the new document
                            xdoc3.Root.Add(id2.Parent)
                        End If
                    Else
                        ' The node in file1 was not found in file2 add this to the new XML file
                        xdoc3.Root.Add(id1.Parent)
                    End If
                End If
            Next

            ' Get a list of all the Nodes that are in fil2 but not in file1 
            Dim notInFirst As List(Of XElement) = (From id In xdoc2.Root.Descendants("Link_ID")
                                                   Where Not xdoc1.Root.Descendants("Link_ID").Any(Function(i) i.Value = id.Value)
                                                   Select id).ToList()

            ' Add all the nodes in the list to the new XML file
            If notInFirst.Count() > 0 Then
                For Each node As XElement In notInFirst
                    xdoc3.Root.Add(node.Parent)
                Next
            End If

            ' Save the document to the file system, change the path location
            xdoc3.Save(Application.StartupPath + "\Data\LinkFiles\LinkFinal.xml")

Open in new window

0
 

Author Closing Comment

by:vcharles
ID: 40566993
Thank You.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error in page 3 45
SQL syntax in VB.net 5 29
VS.net 2010 11 22
VB.net Facial recognition software (time and attendance) 6 16
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

911 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

23 Experts available now in Live!

Get 1:1 Help Now