Solved

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

Posted on 2016-10-02
5
43 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

744 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

12 Experts available now in Live!

Get 1:1 Help Now