Solved

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

Posted on 2015-01-27
8
87 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.NET (2008) - Refactoring Question 2 21
VB.NET - Error - Object not set in Module 4 33
Get number of Files in Directory and Sub Directories 2 42
C# Gridview 1 33
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

777 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