Solved

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

Posted on 2014-11-09
6
128 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 500 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

730 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