Solved

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

Posted on 2014-11-09
6
129 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
Independent Software Vendors: 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!

 

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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

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…
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 Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
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…

729 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