Solved

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

Posted on 2016-11-26
6
73 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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

626 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