Solved

Help with reordering ID data element using VB.NET

Posted on 2015-01-30
8
70 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

734 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