Solved

Help with creating multiple xml files from one xml file using VB.NET

Posted on 2016-11-26
6
20 Views
Last Modified: 2016-12-03
Hi,

How do I create multiple xml files from one xml file using VB.NET? For example If I have an xml file in the following format?
<Root>
   <Table1>
   <SN>10411</10411>
    <Image_SI_1>N/A</Image_SI_1>
    <Image_SI_2>N/A</Image_SI_2>
    <Image_SI_3>N/A</Image_SI_3>
    <Image_SI_4>N/A</Image_SI_4>
    <Image_SIA_1>N/A</Image_SIA_1>
    <Image_SIA_2>N/A</Image_SIA_2>
    <Image_SIA_3>N/A</Image_SIA_3>
    <Image_SIA_4>N/A</Image_SIA_4>
  </Table1>
</Root>

How do I create 3 xml files as follows?

file1.xml
<Root>
   <Table1>
   <SN>10411</10411>
    <Image_SI_1>N/A</Image_SI_1>
    <Image_SI_2>N/A</Image_SI_2>
     </Table1>
</Root>

file2.xml

<Root>
   <Table1>
   <SN>10411</10411>
    <Image_SI_3>N/A</Image_SI_3>
    <Image_SI_4>N/A</Image_SI_4>
    <Image_SIA_1>N/A</Image_SIA_1>
    </Table1>
</Root>

file3.xml

<Root>
   <Table1>
   <SN>10411</10411>
    <Image_SIA_2>N/A</Image_SIA_2>
    <Image_SIA_3>N/A</Image_SIA_3>
    <Image_SIA_4>N/A</Image_SIA_4>
  </Table1>
</Root>

Open in new window


The actual file contains multiple records and the order of the fields needed to create multiple files are in different orders.

Thanks

Victor
0
Comment
Question by:vcharles
  • 2
  • 2
  • 2
6 Comments
 

Expert Comment

by:Krava Krava
Comment Utility
The laptop is disconnected . I changed the password for the account. The monitor is not working
0
 

Expert Comment

by:Krava Krava
Comment Utility
Sorry off topic.
0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
Hi Victor;

You state the following, "The actual file contains multiple records and the order of the fields needed to create multiple files are in different orders.", In order to post a solution to this question we need to know what you mean by multiple records, the node tag types and how do they get separated into the three xml files.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:vcharles
Comment Utility
Hi Fernando,

By multiple records I meant the following example below, also you'll notice Image_SIA_4 is in different order compare to my initial post.

<Root>
    <Table1>
    <SN>10411</SN>
     <Image_SI_1>N/A</Image_SI_1>
     <Image_SIA_4>N/A</Image_SIA_4>
     <Image_SI_2>N/A</Image_SI_2>
     <Image_SI_3>N/A</Image_SI_3>
     <Image_SI_4>N/A</Image_SI_4>
     <Image_SIA_1>N/A</Image_SIA_1>
     <Image_SIA_2>N/A</Image_SIA_2>
     <Image_SIA_3>N/A</Image_SIA_3>
       </Table1>
   <SN>10412</SN>
     <Image_SI_1>N/A</Image_SI_1>
     <Image_SIA_4>N/A</Image_SIA_4>
     <Image_SI_2>N/A</Image_SI_2>
     <Image_SI_3>N/A</Image_SI_3>
     <Image_SI_4>N/A</Image_SI_4>
     <Image_SIA_1>N/A</Image_SIA_1>
     <Image_SIA_2>N/A</Image_SIA_2>
     <Image_SIA_3>N/A</Image_SIA_3>
       </Table1>
   <SN>10414</SN>
     <Image_SI_1>N/A</Image_SI_1>
     <Image_SIA_4>N/A</Image_SIA_4>
     <Image_SI_2>N/A</Image_SI_2>
     <Image_SI_3>N/A</Image_SI_3>
     <Image_SI_4>N/A</Image_SI_4>
     <Image_SIA_1>N/A</Image_SIA_1>
     <Image_SIA_2>N/A</Image_SIA_2>
     <Image_SIA_3>N/A</Image_SIA_3>
       </Table1>
 </Root>

Thanks,

Victor
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
Comment Utility
Hi Victor;

The following code snippet should do what you need.
Dim xdoc = XDocument.Load("C:\Working Directory\Victor2.xml")
Dim file1 = New XDocument(New XElement("Root"))
Dim file2 = New XDocument(New XElement("Root"))
Dim file3 = New XDocument(New XElement("Root"))


Dim grouping1 = New List(Of String)() From {"Image_SI_1", "Image_SI_2"}
Dim grouping2 = New List(Of String)() From {"Image_SI_3", "Image_SI_4", "Image_SIA_1"}
Dim grouping3 = New List(Of String)() From {"Image_SIA_2", "Image_SIA_3", "Image_SIA_4"}

Dim results = From node In xdoc.Root.Elements()
              Let sn = node.Element("SN")
              Let grp1 = (From g1 In node.Elements() Where grouping1.Contains(g1.Name.LocalName) Select g1).OrderBy(Function(n) n.Name.LocalName).ToList()
              Let grp2 = (From g2 In node.Elements() Where grouping2.Contains(g2.Name.LocalName) Select g2).OrderBy(Function(n) n.Name.LocalName).ToList()
              Let grp3 = (From g3 In node.Elements() Where grouping3.Contains(g3.Name.LocalName) Select g3).OrderBy(Function(n) n.Name.LocalName).ToList()
              Select node, sn, grp1, grp2, grp3
			  
For Each node In results
	If node.grp1.Count > 0 Then
    Dim table = New XElement("Table1")
    table.Add(node.sn)
    table.Add(node.grp1)
    file1.Root.Add(table)		
	End If
	If node.grp2.Count > 0 Then
    Dim table = New XElement("Table1")
    table.Add(node.sn)
    table.Add(node.grp2)
    file2.Root.Add(table)
	End If
	If node.grp3.Count > 0 Then
    Dim table = New XElement("Table1")
    table.Add(node.sn)
    table.Add(node.grp3)
    file3.Root.Add(table)
	End If
Next

file1.Save("Path Name for file1")
file2.Save("Path Name for file2")
file3.Save("Path Name for file3")

Open in new window

0
 

Author Closing Comment

by:vcharles
Comment Utility
Thank You.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

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…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

13 Experts available now in Live!

Get 1:1 Help Now