[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
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
Medium Priority
?
107 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 36

Accepted Solution

by:
ste5an earned 2000 total points
ID: 41825184
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
ID: 41825278
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 36

Expert Comment

by:ste5an
ID: 41825298
D'oh? What do you mean? Please elaborate your question..
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:vcharles
ID: 41825321
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 36

Expert Comment

by:ste5an
ID: 41825328
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
ID: 41826952
Thank you.
0

Featured Post

Industry Leaders: 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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

831 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