Solved

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

Posted on 2014-11-09
6
124 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

758 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now