Solved

Help with reordering ID data element using VB.NET

Posted on 2015-01-30
8
67 Views
Last Modified: 2015-02-09
Hi,

How do you reorder the ID data element using VB.NET? For example if my xml file contains

<Root>
<Table>
<ID>4<ID>
<Item></Item>
</Table>
<Table>
<ID>2<ID>
<Item></Item>
</Table>
<Table>
<ID>1<ID>
<Item></Item>
</Table>
<Table>
<ID>3<ID>
<Item></Item>
</Table>
</Root>

How do I obtain the following xml file?

<Root>
<Table>
<ID>1<ID>
<Item></Item>
</Table>
<Table>
<ID>2<ID>
<Item></Item>
</Table>
<Table>
<ID>3<ID>
<Item></Item>
</Table>
<Table>
<ID>4<ID>
<Item></Item>
</Table>
</Root>


Thanks,

Victor
0
Comment
Question by:vcharles
  • 4
  • 4
8 Comments
 
LVL 63

Accepted Solution

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

The following scode snippet will do what you need.

'' Load the XDocument into memory
Dim xdoc = XDocument.Load("C:\Working Directory\Reorder.xml") 

'' Sort the original nodes numerically                                                              
Dim reordered = (From n In xdoc.Descendants("Table")          
                 Order By Integer.Parse(n.Element("ID").Value)
                 Select n).ToList()                           

'' Remove the un-sorted nodes                                                              
xdoc.Root.RemoveAll()        
'' Add the sorted nodes back in.                                 
reordered.ForEach(Sub(n) xdoc.Root.Add(n))                    
'' Save the document
xdoc.Save("C:\Working Directory\Reordered.xml")               

Open in new window

0
 

Author Comment

by:vcharles
ID: 40581034
Thank you, will get back to you after I test the code.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40592860
Hi Victor, how are you doing with this question?
0
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 

Author Closing Comment

by:vcharles
ID: 40593195
Thank You.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40593666
Not a problem Victor, glad to help.
0
 

Author Comment

by:vcharles
ID: 40598030
Hi Fernando,

How do I modify the code to enter numeric values in a data  element (i.e ID) that is empty in squential order?

Thanks,

Victor
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40598380
Hi Victor;

Well you will need to determine what values, number's, will replace the empty nodes values for, <ID>, then sort then. The difficult part is to figure out what ID's to give these nodes.
0
 

Author Comment

by:vcharles
ID: 40598392
Hi Fernando,
I would like to add numbers sequentially starting with 0 in the first record.

Victor
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Suggested Solutions

Title # Comments Views Activity
Checking a checkbox based on SQL DataReader boolean value in ASP.NET(VB) 2 32
location of a form 2 24
System.net.Sockets Error 5 26
Visual studio 2015 1 21
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

808 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