[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2016-10-02
5
Medium Priority
?
70 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

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
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…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

656 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