Solved

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

Posted on 2015-01-27
8
82 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

929 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

11 Experts available now in Live!

Get 1:1 Help Now