?
Solved

Help with comparing xml files

Posted on 2014-11-24
2
Medium Priority
?
67 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 2000 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

Certified OpenStack Administrator Course

We just refreshed our COA course based on the Newton exam.  With 14 labs, this course goes over the different OpenStack services that are part of the certification: Dashboard, Identity Service, Image Service, Networking, Compute, Object Storage, Block Storage, and Orchestration.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

752 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