Solved

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

Posted on 2015-01-27
8
79 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
  • 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

757 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

21 Experts available now in Live!

Get 1:1 Help Now