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

asked on

Help with appending data from file2 to file1 using VB.NET

Hi,

How do I modify the code below to avoid appending a data element from file2 to file1 if it already exit in file1?

Dim f1 = XDocument.Load(Application.StartupPath + "\fil1.xml")
        Dim f2 = XDocument.Load(Application.StartupPath + "\fil2.xml")

        Dim elements As List(Of XElement) = (From n In f2.Descendants("tabl2") _
                                              From c In n.Elements() _
                                              Select c).ToList()

        Dim tables As List(Of XElement) = (From t In f1.Descendants("table1") _
                                           Select t).ToList()

        For Each table As XElement In tables
            For Each element As XElement In elements
                table.Add(element)
            Next
        Next

        f1.Save(Application.StartupPath + "\file3.xml")
Thanks,

Victor
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

how you define if it already existed in file1?

do you have a sample file to show how the content looks like?
Avatar of Victor  Charles

ASKER

Hi,

If file1.xml contains data in Part A and file2.xml data in Part B, I want the code to create the file in Part C by copying data elements from file2.xml to file1.xml when not available in file1.

Part A:

file1.xml

<Root>
<table1>
<ID><I</ID>
<AGD>11</AGD>
<SNN>12</SNN>
</table1>
<table1>
<ID>2</ID>
<SNN>12b</SNN>
<NSC>13b</NSC>
</table1>
<table1>
<ID>3</ID>
<AGD>11c</AGD>
<SNN>12c</SNN>
<NSC>13c</NSC>
</table1>
</Root>

Part B:

file2.xml

<Root>
<table2>
<ID>1</ID>
<NSC></NSC>
<AGD></AGD>
<SNN></SNN>
<THY></THY>


Part C:

file3.xml

<Root>
<table1>
<ID><I</ID>
<AGD>11</AGD>
<SNN>12</SNN>
<NSC></NSC>
<THY></THY>
</table1>
<table1>
<ID>2</ID>
<AGD></AGD>
<SNN>12b</SNN>
<NSC>13b</NSC>
<THY></THY>
</table1>
<table1>
<ID>3</ID>
<AGD>11c</AGD>
<SNN>12c</SNN>
<NSC>13c</NSC>
<THY></THY>
</table1>
</Root>

Thanks,

Victor
Help!
ASKER CERTIFIED SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore 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.