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
91 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
[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
  • 3
  • 3
6 Comments
 
LVL 34

Accepted Solution

by:
ste5an earned 500 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 34

Expert Comment

by:ste5an
ID: 41825298
D'oh? What do you mean? Please elaborate your question..
0
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 

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 34

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

Technology Partners: 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

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…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

717 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