?
Solved

Help with creating multiple xml files from an xml file with multiple tables

Posted on 2014-11-09
6
Medium Priority
?
131 Views
Last Modified: 2014-11-10
Hi,

If I have an xml file listed in part A. How do I create an xml file from data in each table as shown in part B using VB.NET?

Part A:

  <Root>
        <NSC>
              <ID> 1</ID>
                  <CTRY>BEL</CTRY>
              <NSC></NSC>
        </NSC>
        <NSN>
              <ID> 1</ID>
                  <CTRY>CAN</CTRY>
              <NSN></NSN>
        </NSN>
        <FIF>
              <ID> 3</ID>
                  <CTRY>CAN</CTRY>
              <FIF></FIF>
        </FIF>
  </Root>

Part B:

NSC.XML
<Root>
        <NSC>
              <ID> 1</ID>
                  <CTRY>BEL</CTRY>
              <NSC></NSC>
        </NSC>
</Root>

NSN.xml
<Root>
        <NSN>
              <ID> 1</ID>
                  <CTRY>CAN</CTRY>
              <NSN></NSN>
        </NSN>
</Root>

FIF.xml:
<Root>
        <FIF>
              <ID> 3</ID>
                  <CTRY>CAN</CTRY>
              <FIF></FIF>
        </FIF>
  </Root>



Thanks,

Victor
0
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
6 Comments
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 40432228
This should do it:
        Dim xmldoc As New XmlDocument, xmlout As XmlDocument
        xmldoc.Load("XMLFile1.xml")

        For Each xe As XmlElement In xmldoc.DocumentElement.ChildNodes
            xmlout = New XmlDocument
            xmlout.AppendChild(xmlout.CreateElement(xmldoc.DocumentElement.Name))
            xmlout.DocumentElement.AppendChild(xmlout.ImportNode(xe, True))
            xmlout.Save(xe.Name & ".xml")
        Next

Open in new window

0
 

Author Comment

by:vcharles
ID: 40432668
Hi,

How do I save the xml files to a specific path? I need to save them to the  
"xmldoc.Load(Application.StartupPath + "\Data\LinkFiles\" folder.


  Dim xmldoc As New XmlDocument, xmlout As XmlDocument
        xmldoc.Load(Application.StartupPath + "\Data\LinkFiles\LinkFinal" & Me.username.Text & ".xml")

        For Each xe As XmlElement In xmldoc.DocumentElement.ChildNodes
            xmlout = New XmlDocument
            xmlout.AppendChild(xmlout.CreateElement(xmldoc.DocumentElement.Name))
            xmlout.DocumentElement.AppendChild(xmlout.ImportNode(xe, True))
            xmlout.Save(xe.Name & ".xml")
        Next

Thanks,

Victor
0
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 2000 total points
ID: 40432688
You can use the same logic for save as for load:
xmlout.Save(Application.StartupPath + "\Data\LinkFiles\" & xe.Name & ".xml")

Open in new window

0
Stressed Out?

Watch some penguins on the livecam!

 

Author Comment

by:vcharles
ID: 40432779
Thanks for the quick solution, I am using the code below to rename one of the xml files but getting error message "could not complete operation because file already exist" how do you modify the code to overwrite existing LinkRowCountry.xml?

  Dim xmldoc As New XmlDocument, xmlout As XmlDocument
        xmldoc.Load(Application.StartupPath + "\Data\LinkFiles\LinkFinal" & Me.username.Text & ".xml")

        For Each xe As XmlElement In xmldoc.DocumentElement.ChildNodes
            xmlout = New XmlDocument
            xmlout.AppendChild(xmlout.CreateElement(xmldoc.DocumentElement.Name))
            xmlout.DocumentElement.AppendChild(xmlout.ImportNode(xe, True))
            xmlout.Save(Application.StartupPath + "\Data\LinkFiles\Link" & xe.Name & Me.username.Text & ".xml")
            My.Computer.FileSystem.RenameFile((Application.StartupPath + "\Data\LinkFiles\LinkPPF" & Trim(LoginForm1.username.Text) & ".xml"), ("LinkRow" & Trim(LoginForm1.username.Text) & ".xml"))
        Next

Open in new window



Thanks,

Victor
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 40432802
Probably simpler to save directly to the right filename depending on the value of xe.name:
xmlout.Save(Application.StartupPath + "\Data\LinkFiles\Link" & IIf(xe.Name = "PPF", "Row", xe.Name) & Me.username.text & ".xml")

Open in new window

But if you do want to rename the file after the save, first take it outside of the loop and then delete if target already exists (quick and dirty):
            If My.Computer.FileSystem.FileExists(Application.StartupPath + "\Data\LinkFiles\LinkRow" & Trim(LoginForm1.username.text) & ".xml") Then
                My.Computer.FileSystem.DeleteFile(Application.StartupPath + "\Data\LinkFiles\LinkRow" & Trim(LoginForm1.username.text) & ".xml")
            End If
            My.Computer.FileSystem.RenameFile((Application.StartupPath + "\Data\LinkFiles\LinkPPF" & Trim(LoginForm1.username.text) & ".xml"), ("LinkRow" & Trim(LoginForm1.username.text) & ".xml"))

Open in new window

0
 

Author Comment

by:vcharles
ID: 40432834
The first option works perfectly.

Thanks,

Victor
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
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…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

770 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