Solved

Help with combining XML Files using VB.NET

Posted on 2016-08-09
9
56 Views
1 Endorsement
Last Modified: 2016-08-11
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
1
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
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 10

Expert Comment

by:Paweł
ID: 41749822
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.
0
 
LVL 12

Expert Comment

by:funwithdotnet
ID: 41749829
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!
0
 
LVL 12

Expert Comment

by:funwithdotnet
ID: 41749831
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.
0
Command Line Tips and Tricks

The command line is a powerful tool at the disposal of every Linux user. Although Linux distros come with beautiful user interfaces, it's worthwhile to learn the command line because it allows you to do a number of things that you otherwise cannot do from the GUI.  

 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 41750514
Hi Victor;

The following code snippet will do what you need.
'' Make a list of all the names to be compared in the query
Dim FileNames As New List(Of String) From {"..\..\file1.xml", "..\..\file2.xml"}
'' All the node names
Dim fields As List(Of String) = New List(Of String)
'' Go through all the files in the list to get the nodes names
For Each file As String In FileNames
    Dim xdoc = XDocument.Load(file)
    Dim nodeName As List(Of String) = (From f In xdoc.Descendants("Fields").Elements()
                                       Select f.Name.LocalName).ToList()
    fields.AddRange(nodeName)
Next
'' Fields now contains a list of all the Distinct field names
fields = fields.Distinct().ToList()
'' Create the new XML document with Distinct nodes
Dim newDoc = <Root>
                 <Fields>
                     <%= From n In fields
                         Select <<%= n %>/>
                     %>
                 </Fields>
             </Root>
'' Save the XML document to the file system
newDoc.Save("Main.xml")

Open in new window

0
 

Author Comment

by:vcharles
ID: 41750746
Hello,

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

Victor
0
 

Author Comment

by:vcharles
ID: 41751027
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
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 41751263
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

0
 

Author Comment

by:vcharles
ID: 41751328
Thank you.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 41751889
Hi Victor;

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

Thanks
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

627 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