?
Solved

Help with creating xml fles from one xml files

Posted on 2014-08-17
4
Medium Priority
?
248 Views
Last Modified: 2014-08-18
Hi,

How do you create multiple xml files based on a data element?  For example if I have the following xml file:

Final.xml
  <Root>
	  <Table_NSC>
		  <ID> 1</ID>
			<CTRY>BEL</CTRY>
		  <NSC></NSC>
	  </Table_NSC>
	  <Table_NSC>
		  <ID> 2</ID>
			<CTRY>CAN</CTRY>
		  <NSC></NSC>
	  </Table_NSC>
	  <Table_NSC>
		  <ID> 3</ID>
			<CTRY>CAN</CTRY>
		  <NSC></NSC>
	  </Table_NSC>
	  <Table_NSC>
		  <ID> 4</ID>
			<CTRY>DEU</CTRY>
		  <NSC></NSC>
	  </Table_NSC>
  </Root>

Open in new window

How do I create the following xml files?
  <Root>
	  <Table_NSC>
		  <ID> 1</ID>
			<CTRY>BEL</CTRY>
		  <NSC></NSC>
	  </Table_NSC>
</Root>  

Open in new window

CAN.xml
<Root>
	<Table_NSC>
		<ID> 2</ID>
			<CTRY>CAN</CTRY>
		<NSC></NSC>
	</Table_NSC>
	<Table_NSC>
		<ID> 3</ID>
			<CTRY>CAN</CTRY>
		<NSC></NSC>
	</Table_NSC>
</Root>

Open in new window

DEU.xml
<Root>
	<Table_NSC>
		<ID> 4</ID>
			<CTRY>DEU</CTRY>
		<NSC></NSC>
	</Table_NSC>
</Root>  

Open in new window


Thanks,

Victor
0
Comment
Question by:vcharles
3 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 2000 total points
ID: 40267100
Hi Victor, please try this:
Imports System.Xml

Public Class Form1

    Const C_SPLIT As String = "CTRY"

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim objXml As New XmlDocument, objDict As New Dictionary(Of String, XmlDocument)
        objXml.Load("Final.xml")
        For Each objSub As XmlElement In objXml.DocumentElement.ChildNodes
            Dim strSplit = objSub.SelectSingleNode(C_SPLIT).InnerText
            Dim objNewXml As XmlDocument
            If Not objDict.ContainsKey(strSplit) Then
                objNewXml = New XmlDocument
                objNewXml.AppendChild(objNewXml.CreateElement(objXml.DocumentElement.Name))
                objDict.Add(strSplit, objNewXml)
            Else
                objNewXml = objDict.Item(strSplit)
            End If
            objNewXml.DocumentElement.AppendChild(objNewXml.ImportNode(objSub, True))
        Next
        For Each strSub As String In objDict.Keys
            objDict.Item(strSub).Save(strSub & ".xml")
        Next
    End Sub
End Class

Open in new window

0
 
LVL 46

Expert Comment

by:aikimark
ID: 40267184
also, there is probably an XSL transformation for this
0
 

Author Closing Comment

by:vcharles
ID: 40269335
Thank You.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Integration Management Part 2
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

862 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