[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register 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
?
103 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 35

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 35

Expert Comment

by:ste5an
ID: 41825298
D'oh? What do you mean? Please elaborate your question..
0
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!

 

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 35

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

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…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

650 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