Help with adding data elemnts from Fil2to File1 is doesn't already exist in File1 using VB.NET

Hi,

How do I add all data elements in File2 to File1. For example if  File1 contains:


 <Table1>
 <ID>1</ID>
 <DGG>C</DGG>
 <NAS>D</NAS>
 </Table1>
 <Table1>
 <ID>2</ID>
 <DGG>C1</DGG>
 <NAS>D1</NAS>
 </Table1>
 <Table1>
 <ID>3</ID>
 <DGG>C2</DGG>
 <NAS>D2</NAS>
 </Table1>

 and File2 contains:

 <Table2>
 <NASC></NASC>
 <AGD></AGD>
 </Table2>

 How do I create the following file:

 <Table1>
 <ID>1</ID>
 <NASC></NASC>
 <AGD></AGD>
 <DGG>C</DGG>
 <NAS>D</NAS>
 </Table1>
 <Table1>
 <ID>2</ID>
 <NASC></NASC>
 <AGD></AGD>
 <DGG>C1</DGG>
 <NAS>D1</NAS>
 </Table1>
 <Table1>
 <ID>3</ID>
 <NASC></NASC>
 <AGD></AGD>
 <DGG>C2</DGG>
 <NAS>D2</NAS>
 </Table1>

 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.

Fernando SotoRetiredCommented:
Hi Victor;

Here, try this, it should do what you want.
Dim xdoc = XDocument.Load("C:\Working Directory\VictorFile1.xml")
Dim xdocAdd = XDocument.Load("C:\Working Directory\VictorFile2.xml")

Dim addthese = (From n In xdocAdd.Descendants("Table2").Elements()
                Select n).Reverse().ToList()

Dim query = (From n In xdoc.Descendants("Table1").Elements("ID")
             Select n).ToList()

For Each node As XElement In query
    For Each node2 As XElement In addthese
        node.AddAfterSelf(node2)
    Next
Next

xdoc.Save("Path and filename goes here")

Open in new window

vcharlesAuthor Commented:
Hi Fernando,

Thanks.

How do you add the data element only of it doesn't already exist in File1?

Victor
Fernando SotoRetiredCommented:
Hi Victor;

This version of the code should do what you need.
Dim xdoc = XDocument.Load("C:\Working Directory\VictorFile1.xml")
Dim xdocAdd = XDocument.Load("C:\Working Directory\VictorFile2.xml")

Dim addthese = (From n In xdocAdd.Descendants("Table2").Elements()
                Select n).Reverse().ToList()
                
Dim query = (From n In xdoc.Descendants("Table1").Elements("ID")
             From t In addthese
             Where Not n.Parent().Elements().Any(Function(nn) nn.Name.LocalName = t.Name.LocalName)
             Select n, t).ToList()

query.ForEach(Sub(ele) ele.n.Parent().Add(ele.t)) 

xdoc.Save("Path and filename goes here")

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.
Fernando SotoRetiredCommented:
Not a problem Victor, glad to help.
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.