Solved

Help with reordering ID data element using VB.NET

Posted on 2015-01-30
8
64 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 62

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 62

Expert Comment

by:Fernando Soto
ID: 40592860
Hi Victor, how are you doing with this question?
0
 

Author Closing Comment

by:vcharles
ID: 40593195
Thank You.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 62

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 62

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

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)…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

705 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

18 Experts available now in Live!

Get 1:1 Help Now