Link to home
Start Free TrialLog in
Avatar of Victor  Charles
Victor CharlesFlag for United States of America

asked on

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
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

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

Avatar of Victor  Charles

ASKER

Hi Fernando,

Thanks.

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

Victor
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank You.
Not a problem Victor, glad to help.