Solved

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

Posted on 2014-11-09
6
127 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
  • 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Show/Hide ASP.NET controls with JQuery best practices 3 46
VB.Net - KeyPress Event 4 36
Regular expression help 2 25
VB.NET 2008 Publish Error 2 27
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’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

776 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