Solved

Help with comparing xml files

Posted on 2014-11-24
2
65 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
[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 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

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!

Question has a verified solution.

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

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:…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.

732 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