Solved

Help with adding data elements from file b to file a using VB.NET

Posted on 2015-01-27
8
89 Views
Last Modified: 2015-01-29
HI,

How do you add data elements from file B to file A using VB.NET. For example if File A contains

<Root>
<Table>
<ID>1</ID>
<Field1></Field1>
<Field1A></Field1A>
<Field1B></Field1B>
<Field1C></Field1C>
<Table>
<Table>
<ID>2</ID>
<Field1></Field1>
<Field1A></Field1A>
<Table>
<Table>
<ID>3</ID>
<Field1></Field1>
<Field1B></Field1B>
<Field1C></Field1C>
<Table>

and File B contains:

<Table>
<ID>1</ID>
<Field1></Field1>
<Field1A></Field1A>
<Field1B></Field1B>
<Field1C></Field1C>
<Field1D></Field1D>
<Field1E></Field1E>
<Field1F></Field1F>
<Table>

How do I include all data elements from File B to File A if does not already exist in File A?

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
  • 6
  • 2
8 Comments
 

Author Comment

by:vcharles
ID: 40574172
Hi,

How do I modify the code below to only append data element from FileB to FileA if it does not already exist in FileA?

  Dim f1 = XDocument.Load(Application.StartupPath + "\FILEA.xml")
        Dim f2 = XDocument.Load(Application.StartupPath + "\FILEB.xml")

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

        Dim tables As List(Of XElement) = (From t In f1.Descendants("NewDataSet") _
                                           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 + "\File1Updated.xml")

Open in new window

0
 

Author Comment

by:vcharles
ID: 40575162
Help!
0
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 40577729
It often helps to include expected results in your question. It's not 100% clear if you want to match on ID in the tables (solution 1 below) or want to use the one table in FileB to extend all tables with the same fields (solution 2 below).

Also why most experts may have been reluctant to post, the example files are not valid xml, and the example code doesn't match the files (Root vs NewDataSet). This just to try and explain why your question may have gone neglected.

Note that I have assumed <Root></Root> around FileB similar to FileA, may have to be adjusted.

solution 1:
        Dim elements As List(Of XElement) = f2.Descendants("Table").ToList()

        Dim tables As List(Of XElement) = f1.Descendants("Table").ToList()

        For Each table As XElement In tables
            Dim exist() As String = (From d In table.Descendants() Select d.Name.LocalName).ToArray()
            For Each table2 As XElement In elements
                If table2.Descendants("ID").Value = table.Descendants("ID").Value Then
                    For Each element As XElement In table2.Descendants()
                        If Not exist.Contains(element.Name.LocalName) Then
                            table.Add(element)
                        End If
                    Next
                End If
            Next
        Next

Open in new window

Results (only Table with ID 1 extended):
<?xml version="1.0" encoding="utf-8"?>
<Root>
  <Table>
    <ID>1</ID>
    <Field1></Field1>
    <Field1A></Field1A>
    <Field1B></Field1B>
    <Field1C></Field1C>
    <Field1D></Field1D>
    <Field1E></Field1E>
    <Field1F></Field1F>
  </Table>
  <Table>
    <ID>2</ID>
    <Field1></Field1>
    <Field1A></Field1A>
  </Table>
  <Table>
    <ID>3</ID>
    <Field1></Field1>
    <Field1B></Field1B>
    <Field1C></Field1C>
  </Table>
</Root>

Open in new window


Solution 2:
        Dim elements As List(Of XElement) = f2.Descendants("Table").First().Descendants().ToList()

        Dim tables As List(Of XElement) = f1.Descendants("Table").ToList()

        For Each table As XElement In tables
            Dim exist() As String = (From d In table.Descendants() Select d.Name.LocalName).ToArray()
            For Each element As XElement In elements
                If Not exist.Contains(element.Name.LocalName) Then
                    table.Add(element)
                End If
            Next
        Next

Open in new window

Results (all tables extended with all fields from the one table in FileB):
<?xml version="1.0" encoding="utf-8"?>
<Root>
  <Table>
    <ID>1</ID>
    <Field1></Field1>
    <Field1A></Field1A>
    <Field1B></Field1B>
    <Field1C></Field1C>
    <Field1D></Field1D>
    <Field1E></Field1E>
    <Field1F></Field1F>
  </Table>
  <Table>
    <ID>2</ID>
    <Field1></Field1>
    <Field1A></Field1A>
    <Field1B></Field1B>
    <Field1C></Field1C>
    <Field1D></Field1D>
    <Field1E></Field1E>
    <Field1F></Field1F>
  </Table>
  <Table>
    <ID>3</ID>
    <Field1></Field1>
    <Field1B></Field1B>
    <Field1C></Field1C>
    <Field1A></Field1A>
    <Field1D></Field1D>
    <Field1E></Field1E>
    <Field1F></Field1F>
  </Table>
</Root>

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:vcharles
ID: 40577786
Hi,

Thanks for the code, I am looking for solution2, don't want to include a data element from file2 to File1 if it already exist in File 1. I will test the code and get back to you.

Victor
0
 

Author Comment

by:vcharles
ID: 40578109
Hi,

It works for solution2. I also looked into solution 1 with multiple records in fileb, hoping to copy data elements from fileb to fileA only where the IDs are identical, but the data elements are copied to  fileA even when the IDs with fileB don't match.

Thanks,

Victor
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 40578156
If you want solution 1 working as well can you post good test data and expected results please? And/or post your current code because as you can see from my posted output it should work so either I'm misinterpreting your request or you may have copied the code incorrectly.
0
 

Author Comment

by:vcharles
ID: 40578400
Hi,

I will close this post and create another post for solution1 if necessary.

Thank You.

Victor
0
 

Author Closing Comment

by:vcharles
ID: 40578401
Thank You.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

733 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