Solved

Help with comparing xml files

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

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.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

785 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