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
84 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 33

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 33

Expert Comment

by:ste5an
ID: 41825298
D'oh? What do you mean? Please elaborate your question..
0
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 

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 33

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

733 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