Link to home
Start Free TrialLog in
Avatar of Victor  Charles
Victor CharlesFlag for United States of America

asked on

Help with combining XML Files using VB.NET

Hi,

How do create an xml file with fields from 4 other xml files using VB.NET?

For example using two xml files, if the following two xml files contains multiple records with the following data elements:

File1.xml

<Root>
<Fields>
<ID>1</ID
<NSC>A</NSC>
<FIF>B</FIF>
<AGD>C</AGD>
<UVN>D</UVN>
</Fields
<Fields>
<ID>2</ID
<NSC>A1</NSC>
<FIF>B1</FIF>
<AGD>C1</AGD>
<UVN>D1</UVN>
</Fields
</Root>

File2.xml
<Root>
<Fields>
<ID>1</ID
<PFP>A</PFP>
<STL>B</STL>
<BGD>C</BGD>
<UVN>D</UVN>
</Fields
</Root>

How do I create one xml from the two files with no duplicate data elements?

<Root>
<Fields>
<ID>1</ID
<NSC></NSC>
<FIF></FIF>
<AGD></AGD>
<UVN></UVN>
<PFP></PFP>
<STL></STL>
<BGD></BGD>
</Fields
</Root>
Thanks,

victor
Avatar of Paweł
Paweł
Flag of Switzerland image

What i would do is de-serialize the xmls into objects use linq to combine the objects into one array or list then serialize that collection to file.
Avatar of funwithdotnet
funwithdotnet

There's many ways to do that. Many experts use XDocument or XElement to process XML files.

For me, it depends on the schema, content and use. I like to read XML into DataSets. I'm comfortable with processing data using System.Data tools. Whenever the XML schema fits a database structure, I use DataTable.ReadXml.

For writing the XML, I usually do it manually, because the schema isn't usually very database-like. Basically, I just iterate through DataRows, formatting & appending string data. DataTables can write XML, but I think it's easier to get the correct finished product manually.

Good luck!
For uniqueness in elements returned, there are a number of ways to do that. I would just go through all the field in all the data and compile a list of unique columns. Then as the data is read, I can determine if a value should be captured for a given output element.
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Victor  Charles

ASKER

Hello,

Thank you for all the comments. I will try the code and get back to you.

Victor
Hi Fernando,

The code works. Thank You.

is there a way to avoid including table names of the xml files in code below? Each xml file contains different tables.

Dim nodeName As List(Of String) = (From f In xdoc.Descendants(???).Elements()
                                       Select f.Name.LocalName).ToList()
Thanks,

Victor
Hi Victor;

Change the query to this.
Dim nodeName As List(Of String) = (From f In xdoc.Root.Elements().Elements()
                                   Select f.Name.LocalName).ToList()

Open in new window

Thank you.
Hi Victor;

If the solution worked out for you please close the question.

Thanks