Help with combining identical data elements from multuple xml files to create one xml file using VB.NET

Hi,

How do you combine identical data elements from multuple xml files to create one xml file?

For example the following xml files contain:

File1.xml

<Root>
<Table1>
<Item></Item>
<NSC></NSC>
<NAS>X<NAS>
</Table1>
<Table1>
<Item></Item>
<NSC:></NSC>
<NAS>Y<NAS>
</Table1>
<Table1>
<Item></Item>
<NSC:></NSC>
<NAS>Z<NAS>
</Table1>
</Root>

File2.xml

<Root>
<Table2>
<Item></Item>
<NSC></NSC>
<NAS>X1<NAS>
</Table2>
<Table2>
<Item></Item>
<NSC:></NSC>
<NAS>Y1<NAS>
</Table2>
<Table2>
<Item></Item>
<NSC:></NSC>
<NAS>Z1<NAS>
</Table2>
</Root>

How do you cretae

FileNAS.xml

<Root>
<Table1>
<NAS>X<NAS>
</Table1>
<Table1>
<NAS>Y<NAS>
</Table1>
<Table1>
<NAS>Z<NAS>
</Table1>
<Table1>
<NAS>X1<NAS>
</Table1>
<Table1>
<NAS>Y1<NAS>
</Table1>
<Table1>
<NAS>Z1<NAS>
</Table1>
</Root>

Thanks,

Victor
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.

Lokesh B RDeveloperCommented:
Hi,

Find the sample here.

VB.NET

Dim xml As New XmlDocument()
'Load first file and select the nodes
xml.Load("D:\....\File1.xml")
Dim xnList As XmlNodeList = xml.SelectNodes("/Root/Table1/NAS")

'Load second file and select the nodes
xml = New XmlDocument()
xml.Load("D:\...\File2.xml")
Dim xnList1 As XmlNodeList = xml.SelectNodes("/Root/Table2/NAS")

Dim relpath As String = Server.MapPath("~/Output/FileNAS.xml")

Dim xwrite As XmlWriter = XmlWriter.Create(relpath)
xwrite.WriteStartDocument()
xwrite.WriteStartElement("Root")

For Each node As XmlNode In xnList
	xwrite.WriteStartElement("Table1")
	xwrite.WriteElementString("NAS", node.InnerText)
	xwrite.WriteEndElement()
Next

For Each node As XmlNode In xnList1
	xwrite.WriteStartElement("Table1")
	xwrite.WriteElementString("NAS", node.InnerText)
	xwrite.WriteEndElement()
Next

xwrite.WriteEndElement()
xwrite.WriteEndDocument()
xwrite.Close()

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
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
Visual Basic.NET

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.