Solved

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

Posted on 2016-10-02
5
52 Views
Last Modified: 2016-10-05
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
0
Comment
Question by:vcharles
  • 3
  • 2
5 Comments
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 41825741
how you define if it already existed in file1?

do you have a sample file to show how the content looks like?
0
 

Author Comment

by:vcharles
ID: 41825764
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
0
 

Author Comment

by:vcharles
ID: 41826936
Help!
0
 
LVL 49

Accepted Solution

by:
Ryan Chong earned 500 total points
ID: 41829294
try this:

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("table2") _
                                              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
                Dim t = table.Element(element.Name)
                If t Is Nothing Then
                    table.Add(element)
                End If

            Next
        Next

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

Open in new window

0
 

Author Closing Comment

by:vcharles
ID: 41830563
Thank You.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Visual Studio .Net 2012 and Crystal Reports 9 45
C# HTTP GET method sample code 3 40
how to read json value 2 30
Variable Event ? 3 22
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

911 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now