Solved

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

Posted on 2016-11-26
6
62 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
[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
  • 2
  • 2
  • 2
6 Comments
 

Expert Comment

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

Expert Comment

by:Krava Krava
ID: 41902243
Sorry off topic.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 41902300
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
Technology Partners: 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: 41902425
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 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 41902547
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
ID: 41902612
Thank You.
0

Featured Post

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!

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

763 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