?
Solved

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

Posted on 2016-10-02
5
Medium Priority
?
64 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 53

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 53

Accepted Solution

by:
Ryan Chong earned 2000 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

752 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