Solved

Help with comparing xml files

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

757 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