Solved

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

Posted on 2016-11-26
6
49 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
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …

832 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