Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 74
  • Last Modified:

Help with reordering ID data element using VB.NET

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
vcharles
Asked:
vcharles
  • 4
  • 4
1 Solution
 
Fernando SotoRetiredCommented:
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
 
vcharlesAuthor Commented:
Thank you, will get back to you after I test the code.
0
 
Fernando SotoRetiredCommented:
Hi Victor, how are you doing with this question?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
vcharlesAuthor Commented:
Thank You.
0
 
Fernando SotoRetiredCommented:
Not a problem Victor, glad to help.
0
 
vcharlesAuthor Commented:
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
 
Fernando SotoRetiredCommented:
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
 
vcharlesAuthor Commented:
Hi Fernando,
I would like to add numbers sequentially starting with 0 in the first record.

Victor
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now