?
Solved

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

Posted on 2015-01-27
8
Medium Priority
?
99 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 2000 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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 

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

How To Install Bash on Windows 10

Windows’ budding partnership with Canonical has certainly led to some great improvements. One of them being the ability to use Bash on your Windows machine without third party applications! This might be one of the greatest things a cloud engineer in a Windows environment can do!

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

771 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