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

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
vcharlesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
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
vcharlesAuthor Commented:
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
Fernando SotoRetiredCommented:
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
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

vcharlesAuthor Commented:
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
Fernando SotoRetiredCommented:
The examples you just gave are not well formed. What would you like the resulting modified file to look like.
0
vcharlesAuthor Commented:
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
Fernando SotoRetiredCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vcharlesAuthor Commented:
Thank you.
0
vcharlesAuthor Commented:
It Works Perfectly!

Thank You again.

Victor
0
vcharlesAuthor Commented:
Thank You!
0
Fernando SotoRetiredCommented:
Not a problem Victor, glad to help.
0
vcharlesAuthor Commented:
Hi Fernando,

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

Thanks,

Victor
0
Fernando SotoRetiredCommented:
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
vcharlesAuthor Commented:
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
Fernando SotoRetiredCommented:
Hi Victor;

Please open another question for this seeming this question is already closed, meanwhile I will look at this question
0
vcharlesAuthor Commented:
Ok, Will do.

Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.