Solved

Help with comparing two xml file and transferring data to file based on identical values using VB.NET

Posted on 2016-10-01
6
59 Views
Last Modified: 2016-10-03
Hi,

How do I copy data from two data elements (FCT, FCI) file2 to two empty data elements (FCT, FCI) in file1 if values in 3 data elements (Receiver, AGD, NSC)  in both files are identical and include results in a new file, (i.e. file3.xml), using VBNET?

For example if file1 contains:

File1.xml
<?xml version="1.0" encoding="utf-8"?>
<Root>
<Table1>
  <Link_ID>1</Link_ID>
  <Receiver>USA</Receiver>
  <AGD>M5</AGD>
<NSC><M2</NSC>
  <FCT></FCT>
  <FCI></FCI>
<NFZ>M9</NFZ>
</Table1>
<Table1>
  <Link_ID>2</Link_ID>
  <Receiver>BEL</Receiver>
  <AGD>M5</AGD>
<NSC>M2</NSC>
  <FCT></FCT>
  <FCI></FCI>
 <NFZ>M10</NFZ>
</Table1>
<Table1>
  <Link_ID>3</Link_ID>
  <Receiver>USA</Receiver>
  <AGD>M5</AGD>
<NSC><M2</NSC>
  <FCT></FCT>
  <FCI></FCI>
<NFZ>M11</NFZ>
</Table1>
</Root>


File2.xml

<?xml version="1.0" encoding="utf-8"?>
<Root>
<FCD>
  <ID>1</ID>
  <Receiver>USA</Receiver>
  <AGD>M5</AGD>
<NSC><M2</NSC>
  <FCT>M11</FCT>
  <FCI>M14</FCI>
<ADO>M3</ADO>
<CCD>M9</CCD>
</FCD>
<FCD>
  <ID>2</ID>
  <Receiver>BEL</Receiver>
  <AGD>M5</AGD>
<NSC><M23</NSC>
  <FCT>M3</FCT>
  <FCI>M77</FCI>
<ADO>M45</ADO>
<CCD>M10</CCD>
</FCD>
<FCD>
  <ID>3</ID>
  <Receiver>BEL</Receiver>
  <AGD>M5</AGD>
<NSC><M2</NSC>
  <FCT>M33</FCT>
  <FCI>M35</FCI>
<ADO>M36</ADO>
<CCD>M56</CCD>
</FCD>
</Root>

How do I  save new values of file1 in file3.xml as shown below since only one record match in both files?

File3.xml
<?xml version="1.0" encoding="utf-8"?>
<Root>
<Table1>
  <Link_ID>1</Link_ID>
  <Receiver>USA</Receiver>
  <AGD>M5</AGD>
<NSC><M2</NSC>
  <FCT>M11</FCT>
  <FCI>M14</FCI>
<NFZ>M9</NFZ>
</Table1>
<Table1>
  <Link_ID>2</Link_ID>
  <Receiver>BEL</Receiver>
  <AGD>M5</AGD>
<NSC>M2</NSC>
  <FCT></FCT>
  <FCI></FCI>
 <NFZ>M10</NFZ>
</Table1>
<Table1>
  <Link_ID>3</Link_ID>
  <Receiver>USA</Receiver>
  <AGD>M5</AGD>
<NSC><M2</NSC>
  <FCT>M11</FCT>
  <FCI>M14</FCI>
<NFZ>M11</NFZ>
</Table1>
</Root>
0
Comment
Question by:vcharles
  • 3
  • 3
6 Comments
 
LVL 32

Accepted Solution

by:
Stefan Hoffmann earned 500 total points
Comment Utility
First of all: Use the CODE button and post valid XML.

With XML you've got lot of options, but I prefer working with XDocument. E.g.

Imports System.Xml.XPath

Module Module1
    Sub Main()
        Dim Xml1 As XDocument = GetXml1()
        Dim Xml2 As XDocument = GetXml2()

        For Each Table1 As XElement In Xml1.XPathSelectElements("/Root/Table1")
            Dim ID As String = Table1.XPathSelectElement("Link_ID").Value
            Dim Fcd As XElement = Xml2.XPathSelectElement("/Root/FCD[ID=" & ID & "]")
            Table1.XPathSelectElement("FCT").Value = Fcd.XPathSelectElement("FCT").Value
            Table1.XPathSelectElement("FCI").Value = Fcd.XPathSelectElement("FCI").Value
        Next

        For Each Table1 As XElement In Xml1.XPathSelectElements("/Root/Table1")
            Console.WriteLine(Table1)
        Next

        Console.WriteLine("Done.")
        Console.ReadLine()
    End Sub
    Function GetXml1() As XDocument
        Dim Xml As String = "
            <Root>
                <Table1>
                    <Link_ID>1</Link_ID>
                    <Receiver>USA</Receiver>
                    <AGD>M5</AGD>
                    <NSC>M2</NSC>
                    <FCT></FCT>
                    <FCI></FCI>
                    <NFZ>M9</NFZ>
                </Table1>
                <Table1>
                    <Link_ID>2</Link_ID>
                    <Receiver>BEL</Receiver>
                    <AGD>M5</AGD>
                    <NSC>M2</NSC>
                    <FCT></FCT>
                    <FCI></FCI>
                    <NFZ>M10</NFZ>
                </Table1>
                <Table1>
                    <Link_ID>3</Link_ID>
                    <Receiver>USA</Receiver>
                    <AGD>M5</AGD>
                    <NSC>M2</NSC>
                    <FCT></FCT>
                    <FCI></FCI>
                    <NFZ>M11</NFZ>
                </Table1>
            </Root>"
        GetXml1 = XDocument.Parse(Xml)
    End Function

    Function GetXml2() As XDocument
        Dim Xml As String = "
            <Root>
                <FCD>
	                <ID>1</ID>
	                <Receiver>USA</Receiver>
	                <AGD>M5</AGD>
	                <NSC>M2</NSC>
	                <FCT>M11</FCT>
	                <FCI>M14</FCI>
	                <ADO>M3</ADO>
	                <CCD>M9</CCD>
                </FCD>
                <FCD>
	                <ID>2</ID>
	                <Receiver>BEL</Receiver>
	                <AGD>M5</AGD>
	                <NSC>M23</NSC>
	                <FCT>M3</FCT>
	                <FCI>M77</FCI>
	                <ADO>M45</ADO>
	                <CCD>M10</CCD>
                </FCD>
                <FCD>
	                <ID>3</ID>
	                <Receiver>BEL</Receiver>
	                <AGD>M5</AGD>
	                <NSC>M2</NSC>
	                <FCT>M33</FCT>
	                <FCI>M35</FCI>
	                <ADO>M36</ADO>
	                <CCD>M56</CCD>
                </FCD>
            </Root>"
        GetXml2 = XDocument.Parse(Xml)
    End Function
End Module

Open in new window

0
 

Author Comment

by:vcharles
Comment Utility
Hi

Is there awayy to solve this by using the names of the xml files without hardcoding the contents of the xml files?

Thanks,

Victor
0
 
LVL 32

Expert Comment

by:Stefan Hoffmann
Comment Utility
D'oh? What do you mean? Please elaborate your question..
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:vcharles
Comment Utility
I meant, can we use Dim f1 = XDocument.Load("C:\Test\file1.xml") instead of GetXML function. Also I don't see in the code where  values in Receiver, AGD, NSC habe to be the same to copy values from FCI and FCT to file1.

Thanks,

Victor
0
 
LVL 32

Expert Comment

by:Stefan Hoffmann
Comment Utility
I meant, can we use Dim f1 = XDocument.Load("C:\Test\file1.xml") instead of GetXML function.

Just try it..

Also I don't see in the code where  values in Receiver, AGD, NSC habe to be the same to copy values from FCI and FCT to file1.

Why? They are the same. Otherwise just copy them also.
0
 

Author Closing Comment

by:vcharles
Comment Utility
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

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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: …
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 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

15 Experts available now in Live!

Get 1:1 Help Now