Solved

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

Posted on 2013-10-27
9
509 Views
Last Modified: 2013-10-28
Hi,

If I have an xml file in the following format:


<Root>
<Table1>
<Link_ID></lINK_ID>
<Item></Item>
</Table1>
<Table2>
<Link_ID></lINK_ID>
<Item></Item>
</Table2>
<Table3>
<Link_ID></lINK_ID>
<Item></Item>
</Table3>
</Root>

How do I create another xml for each table?

File1.xml:
<Root>
<Table1>
<Link_ID></lINK_ID>
<Item></Item>
</Table1>
</Root>

File2.xml:
<Root>
<Table2>
<Link_ID></lINK_ID>
<Item></Item>
</Table2>
</Root>

File3.xml:
<Root>
<Table3>
<Link_ID></lINK_ID>
<Item></Item>
</Table3>
</Root>

Thanks,

Victor
0
Comment
Question by:vcharles
  • 5
  • 2
  • 2
9 Comments
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 39604670
Hi Victor;

This code snippet should give the results you are looking for.

' Load original XML document
Dim xdoc = XElement.Load("C:\Working Directory\AllTables.xml")
' Get all the tableX nodes and there children
Dim tables = From t in xdoc.Descendants() _
             Where t.Name.ToString().StartsWith("Table")
             Select t

' Set up the new XML document and save to the file system             
For Each table In tables
    ' Get the Table number so that we can create the file name with that number
    Dim fileNumber As String = table.Name.ToString().SubString(5)
    ' Create a new Root node and add the TableX as its child
    Dim newRoot = New XElement("Root", table)
    ' Save the new XML document to the file system
    newRoot.Save("C:\Working Directory\File" + fileNumber + ".xml") 
Next

Open in new window

0
 
LVL 15

Expert Comment

by:Ess Kay
ID: 39604743
You can use regular expressions

Run a loop with the number of tables

With pattern "<table" & Counter & ">*</table" & Counter & ">"
0
 
LVL 15

Expert Comment

by:Ess Kay
ID: 39604747
Sorry,  scrap the loop,  just the pattern
"<table*</table.>"

Each match from regex will be the entire table.  Save it to xml file like

For each match in regex(xmlfile,  pattern)
Save xml
0
 

Author Comment

by:vcharles
ID: 39604792
Thank you for all the comments, will test the code and get back to you.
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
ID: 39604848
Hi,

I received error on line. Row is the name of the first table

Dim fileNumber As String = table.Name.ToString().Substring(5)

Error:

startIndex cannot be larger than length of string.
Parameter name: startIndex

 Dim xdoc = XElement.Load(Application.StartupPath & "\LinkFinal.xml")
        ' Get all the tableX nodes and there children
        Dim tables = From t In xdoc.Descendants() _
                     Where t.Name.ToString().StartsWith("Row")
                     Select t

        ' Set up the new XML document and save to the file system            
        For Each table In tables
            ' Get the Table number so that we can create the file name with that number
            Dim fileNumber As String = table.Name.ToString().Substring(5)
            ' Create a new Root node and add the TableX as its child
            Dim newRoot = New XElement("Root", table)
            ' Save the new XML document to the file system
            'newRoot.Save("C:\Working Directory\File" + fileNumber + ".xml")
            newRoot.Save(Application.StartupPath & fileNumber & ".xml")
0
 

Author Closing Comment

by:vcharles
ID: 39605222
It works.

Thank You.

Victor
0
 

Author Comment

by:vcharles
ID: 39605401
Hi,

Is it possible to use the same code and loop through multiple tables in a string rather than writing the same code for multiple tables?

For example, If I include the tables in a string variable s = Name,Item,Location,Tasks

How do I use s in the code to save an xml file for each table?

Victor
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 39605598
Hi Victor;

To your question, "Is it possible to use the same code and loop through multiple tables in a string rather than writing the same code for multiple tables?", you can't use the exact same code, the code will need to change to handle the different format of the source data.

To your question, "For example, If I include the tables in a string variable s = Name,Item,Location,Tasks. How do I use s in the code to save an xml file for each table?", to answer this question I would need to know if all the fields Name,Item,Location,Tasks will always be in the same order in the string and whether or not all fields will always be in the string for each table. Basically you would enumerate through the fields in the string and construct an XElement and build the TableX node and then use the XElement.Save() method to save to a file.

If you will be needing help to do this please open a new question.  Thank you.
0
 

Author Comment

by:vcharles
ID: 39605639
Hi,

Yes, all the fileds will alsway be in the same order.  I will open a new questions.

Thanks.
0

Featured Post

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!

Join & Write a Comment

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)…
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

706 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

23 Experts available now in Live!

Get 1:1 Help Now