Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2016-11-26
6
Medium Priority
?
85 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 64

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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 

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 64

Accepted Solution

by:
Fernando Soto earned 2000 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Integration Management Part 2
Loops Section Overview
Suggested Courses
Course of the Month11 days, 14 hours left to enroll

916 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