Solved

Help with reordering ID data element using VB.NET

Posted on 2015-01-30
8
68 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

679 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