[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Help with appending data from file2.xml to file1.xml using VB.NET

Posted on 2014-08-15
16
Medium Priority
?
213 Views
Last Modified: 2015-01-29
Hi,

If file1 contains

<Root>
<Table1>
<ID>1</ID>
<ItemA></ItemA>
<ItemB></ItemB>
</Table1>
<Table1>
<ID>2</ID>
<ItemA></ItemA>
<ItemB></ItemB>
</Table1>
<Table1>
<ID>3</ID>
<ItemA></ItemA>
<ItemB></ItemB>
</Table1>

and file 2 contains

<Root>
<Table2>
<ItemX></ItemX>
<ItemY></ItemY>
<ItemZ></ItemZ>
</Table2>

How of I append all the data elements from file2 to table1 of file1 using VB.NET?


<Root>
<Table1>
<ID>1</ID>
<ItemA></ItemA>
<ItemB></ItemB>
<ItemX></ItemX>
<ItemY></ItemY>
<ItemZ></ItemZ>
</Table1>
<Table1>
<ID>2</ID>
<ItemA></ItemA>
<ItemB></ItemB>
<ItemX></ItemX>
<ItemY></ItemY>
<ItemZ></ItemZ>
</Table1>
<Table1>
<ID>3</ID>
<ItemA></ItemA>
<ItemB></ItemB>
<ItemX></ItemX>
<ItemY></ItemY>
<ItemZ></ItemZ>
</Table1>



Thanks,

Victor


and File2.xml contains
0
Comment
Question by:vcharles
  • 9
  • 7
16 Comments
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 40263563
Hi Victor;

The following code should do what you need.

Dim f1 = XDocument.Load("Path-To-XML-File\File1.xml")
Dim f2 = XDocument.Load("Path-To-XML-File\File2.xml")

Dim elements As List(Of XElement) = (From n In f2.Descendants() _
                                     Where n.Name.ToString().StartsWith("Item") _
                                     Select n).ToList()

Dim tables As List(Of XElement) = (From t In f1.Descendants() _
                                   Where t.Name.ToString().StartsWith("Table") _
                                   Select t).ToList() 
              
For Each table As XElement in tables
    For Each element As XElement in elements
        table.Add(element)
    Next
Next

f1.Save("Path-To-XML-File\File1Updated.xml")

Open in new window

0
 

Author Comment

by:vcharles
ID: 40263988
Hi,

Unfortunately it did not work, should the table names be changed? I don't have a table named "Item/Table" in my xml files.

Thanks,

Victor
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 40264034
XML is a case sensitive language when accessing XML documents through Visual Basic node names must be in the same case even though variable names in Visual Basic need not be in the same case.

The only two lines that would need to be modified are the following

' Where Item below are nodes in file2 that look like ItemX where X is the only characters that differ from line to line.
Where n.Name.ToString().StartsWith("Item") _

' Where Table is the node name in file1 where 1 may or may not change from table to table.
Where t.Name.ToString().StartsWith("Table") _

The code will work if the schema of the file matches the code.
0
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!

 

Author Comment

by:vcharles
ID: 40264065
Hi,

I see you what you mean, the problem is the Item names are different. For example my xml files are as follows: I know how to append individual nodes but since the xml file I need to append contains about 40 data elemennts, would be best to do it a smarter way.

I'm sorry for the confusion.

File1.xml

<Root>
 <AOP5>
 <ID>1</ID>
 <CTRY></CTRY>
 <NSC></NSC>
 </AOP5>
<AOP5>
 <ID>2</ID>
 <CTRY></CTRY>
 <NSC></NSC>
 </AOP5>
<AOP5>
 <ID>3</ID>
 <CTRY></CTRY>
 <NSC></NSC>
 </AOP5>


File2.xml

and file 2 contains

 <Root>
 <AOP5B>
 <FIF></FIF>
 <NMN></NMN>
 <Donor></Donor>
 </Table2>
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 40264079
The examples you just gave are not well formed. What would you like the resulting modified file to look like.
0
 

Author Comment

by:vcharles
ID: 40264143
Hi,

Based on the example, it should look like the following:

File1.xml

 <Root>
  <AOP5>
  <ID>1</ID>
  <CTRY></CTRY>
  <NSC></NSC>
  <FIF></FIF>
  <NMN></NMN>
  <Donor></Donor>
  </AOP5>
 <AOP5>
  <ID>2</ID>
  <CTRY></CTRY>
  <NSC></NSC>
  <FIF></FIF>
  <NMN></NMN>
  <Donor></Donor>
  </AOP5>
 <AOP5>
  <ID>3</ID>
  <CTRY></CTRY>
  <NSC></NSC>
  <FIF></FIF>
  <NMN></NMN>
  <Donor></Donor>
  </AOP5>
 </Root>

Thanks,

Victor
0
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 40264221
Hi Victor;

Try it like this.

Dim f1 = XDocument.Load("Path-To-XML-File\File1.xml")                
Dim f2 = XDocument.Load("Path-To-XML-File\File2.xml")                
                                                                         
Dim elements As List(Of XElement)  = (From n In f2.Descendants("AOP5B") _
                                      From c In n.Elements() _           
                                      Select c).ToList()                 
                                                                         
Dim tables As List(Of XElement) = (From t In f1.Descendants("AOP5") _    
                                   Select t).ToList()                    
                                                                         
For Each table As XElement in tables                                     
    For Each element As XElement in elements                             
        table.Add(element)                                               
    Next                                                                 
Next                                                                     
                                                                                                                         
f1.Save("Path-To-XML-File\File1Updated.xml")                            

Open in new window

0
 

Author Comment

by:vcharles
ID: 40264322
Thank you.
0
 

Author Comment

by:vcharles
ID: 40264563
It Works Perfectly!

Thank You again.

Victor
0
 

Author Closing Comment

by:vcharles
ID: 40264564
Thank You!
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 40264583
Not a problem Victor, glad to help.
0
 

Author Comment

by:vcharles
ID: 40576234
Hi Fernando,

Is there a way to append the data to file1 only if it does not already exist.

Thanks,

Victor
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 40576662
Hi Victor;

I am sure something can be done but to give a better answer I need to see what the file1 looks like and what you mean by "only if it does not already exist." XML is a very precise language and so to get it correct we need to know.
0
 

Author Comment

by:vcharles
ID: 40577492
Hi Fernando,

If file A and B contain the data shown, I would like to create File C.

File A:

<Root>
<Table1>
<ID>1</ID>
<ItemA>M101</ItemA>
<ItemB>M102</ItemB>
<ItemC>M103</ItemA>
<ItemD>M104</ItemB>
</Table1>
<Table1>
<ID>2</ID>
<ItemA></ItemA>
<ItemB></ItemB>
</Table1>
<Table1>
<ID>3</ID>
<ItemA></ItemA>
<ItemB></ItemB>
<ItemC></ItemC>
</Table1>

File B:

<Root>
<Table2>
<ItemA></ItemA>
<ItemB></ItemB>
<ItemC></ItemC>
<ItemD></ItemD>
</Table2>

File C:

<Root>
<Table1>
<ID>1</ID>
<ItemA>M101</ItemA>
<ItemB>M102</ItemB>
<ItemC>M103</ItemA>
<ItemD>M104</ItemB>
</Table1>
<Table1>
<ID>2</ID>
<ItemA></ItemA>
<ItemB></ItemB>
<ItemC></ItemC>
<ItemD></ItemD>
</Table1>
<Table1>
<ID>3</ID>
<ItemA></ItemA>
<ItemB></ItemB>
<ItemC></ItemC>
<ItemD></ItemD>

I only want to add data element from File B to File A if it does not already exist in File A.

Thanks,

Victor
</Table1>
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 40577538
Hi Victor;

Please open another question for this seeming this question is already closed, meanwhile I will look at this question
0
 

Author Comment

by:vcharles
ID: 40577607
Ok, Will do.

Thanks.
0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
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…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Suggested Courses
Course of the Month18 days, 1 hour left to enroll

830 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