Help with appending partial data from file2 to file1 based on identical IDs using VB.NET

Hi,

How to I copy the value of some of the data elements from file2 to file1 based on identical IDs? For example if file 1 contains:

 <Root>
 <Table1>
 <ID>1</ID>
 <SN>10411</SN>
 <ItemA>DATA</ItemA>
 <ItemX>DATAX</ItemX>
 <ItemY>DATAY</ItemY>
 <ItemZ>DATAZ</ItemZ>
 </Table1>
 <Table1>
 <ID>2</ID>
<SN>10411</SN>
 <ItemA>DATA1</ItemA>
 <ItemX>DATAX1</ItemX>
 <ItemY>DATAY1</ItemY>
 <ItemZ>DATAZ1</ItemZ>
 </Table1>
 <Table1>
<ID>3</ID>
<SN>10411</SN>
 <ItemA>DATA2</ItemA>
 <ItemX>DATAX2</ItemX>
 <ItemY>DATAY2ItemY>
 <ItemZ>DATAZ2</ItemZ>
 </Table1>
 </Root>
 
and file2 contains:

 <Root>
 <Table1>
<ID>1</ID>
 <SN>10411<SN>
 <ItemXX>A</ItemXX>
 <ItemYY>B</ItemYY>
 <ItemZZ>C</ItemZZ>
</Table1>
<Table1>
<ID>2</ID>
 <SN>10411<SN>
 <ItemXX>D</ItemXX>
 <ItemYY>E</ItemYY>
 <ItemZZ>F</ItemZZ>
</Table1>
<Table1>
<ID>3</ID>
 <SN>10411<SN>
 <ItemXX>G</ItemXX>
 <ItemYY>H</ItemYY>
 <ItemZZ>I</ItemZZ>
 </Table1>
 </Root>

 New File 1 should contain:
<Root>
 <Table1>
 <ID>1</ID>
 <SN>10411</SN>
 <ItemA>DATA</ItemA>
 <ItemX>DATAX</ItemX>
 <ItemY>DATAY</ItemY>
 <ItemZ>DATAZ</ItemZ>
 <ItemXX>A</ItemXX>
 <ItemYY>B</ItemYY>
 <ItemZZ>C</ItemZZ>
 </Table1>
 <Table1>
 <ID>2</ID>
<SN>10411</SN>
 <ItemA>DATA1</ItemA>
 <ItemX>DATAX1</ItemX>
 <ItemY>DATAY1</ItemY>
 <ItemZ>DATAZ1</ItemZ
<ItemXX>D</ItemXX>
 <ItemYY>E</ItemYY>
 <ItemZZ>F</ItemZZ>
 </Table1>
 <Table1>
<ID>3</ID>
<SN>10411</SN>
 <ItemA>DATA2</ItemA>
 <ItemX>DATAX2</ItemX>
 <ItemY>DATAY2ItemY>
 <ItemZ>DATAZ2</ItemZ>
<ItemXX>G</ItemXX>
 <ItemYY>H</ItemYY>
 <ItemZZ>I</ItemZZ>
 </Table1>
 </Root>

Thanks,

Victor
vcharlesAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

vcharlesAuthor Commented:
Hi,

The code below works but I'm getting duplicate ID and SN data elements. How do I modify the code not to include SN and ID data elements from file2?
  ' Load the file1.xml and file2.xml into memory
        Dim f1 = XDocument.Load(Application.StartupPath + "\AOP40SC1X.xml")
        Dim f2 = XDocument.Load(Application.StartupPath + "\AOP40SC2A.xml")
        ' Query both documents and Join on the SN node in each 
        Dim results = From n1 In f1.Descendants("Table1") _
                      Where n1.Element("ID") IsNot Nothing _
                      From n2 In f2.Descendants("Table1") _
                      Where n2.Element("ID") IsNot Nothing AndAlso n1.Element("ID").Value = n2.Element("ID").Value _
                      Select n1, n2

        ' Copy the elements from File2 to File1
        For Each pair In results
            Dim removeSN = pair.n2.Element("ID")
            removeSN.Remove()
            For Each element As XElement In pair.n2.Descendants()
                pair.n1.Add(element)
            Next
        Next
        '' Save the modified document
        f1.Save(Application.StartupPath + "\AOPmerged.xml")

Open in new window

Thanks
Fernando SotoRetiredCommented:
Hi Victor;

The code you posted was just removing the ID node where it needed to remove both the ID and SN nodes. The code below reflects that change.

' Load the file1.xml and file2.xml into memory
Dim f1 = XDocument.Load(Application.StartupPath + "\AOP40SC1X.xml")
Dim f2 = XDocument.Load(Application.StartupPath + "\AOP40SC2A.xml")

' Query both documents and Join on the SN node in each 
Dim results = From n1 In f1.Descendants("Table1") _
              Where n1.Element("ID") IsNot Nothing _
              From n2 In f2.Descendants("Table1") _
              Where n2.Element("ID") IsNot Nothing AndAlso n1.Element("ID").Value = n2.Element("ID").Value _
              Select n1, n2

' Copy the elements from File2 to File1
For Each pair In results
    Dim removeSN = pair.n2.Element("SN")
    Dim removeID = pair.n2.Element("ID")
    removeSN.Remove()
    removeID.Remove()
    For Each element As XElement In pair.n2.Descendants()
        pair.n1.Add(element)
    Next
Next

'' Save the modified document
f1.Save(Application.StartupPath + "\AOPmerged.xml")

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vcharlesAuthor Commented:
Thank You.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.