Link to home
Create AccountLog in
Avatar of Victor  Charles
Victor CharlesFlag for United States of America

asked on

Help with sortin ID element in XML file using VB.NET

Hi,

After combining multiple xml files my LinkTotal.xml contains multiple identical IDs. How do I
loop through the xml file and reorder the ID values?

For example if I have the following

<ID>1</ID<
<Item>A</Item>

<ID>1</ID<
<Item>B</Item>

<ID>1</ID<
<Item>C</Item>

<ID>1</ID<
<Item>D</Item>

I need to reorder the ID in sequential order

<ID>1</ID<
<Item>A</Item>

<ID>2</ID<
<Item>B</Item>

<ID>3</ID<
<Item>C</Item>

<ID>4</ID<
<Item>D</Item>

Thanks,

Victor
Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

can u post the whole xml?
also is item element  is child of ID or sibling of ID?
The easiest solution

Dim dt as datatable

dt.TableName = "Video"

dt.ReadXML("C:\Video.XML")

to sort

dt.DefaultView.Sort = "ID ASC"

 

to again write

Dim dtNew As DataTable = dt.DefaultView.ToTable
dtNew .WriteXml("c:\data2.xml")


Example found at http://forums.asp.net/t/1368197.aspx/1
ASKER CERTIFIED SOLUTION
Avatar of Easwaran Paramasivam
Easwaran Paramasivam
Flag of India image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of Victor  Charles

ASKER

Hi,

Sorry I can not post the xml file.

I was able to sort using the code below, how do I start the sort at 1? ít is starting at 0.

Dim xdoc As New Xml.XmlDocument
        Dim nodes As Xml.XmlNodeList

        xdoc.Load(Application.StartupPath & "\LinkTOTAL.xml")
        nodes = xdoc.SelectNodes("//LinkAID")

        For i As Integer = 0 To nodes.Count - 1
            nodes(i).InnerText = i.ToString()
        Next

        xdoc.Save(Application.StartupPath & "\LinkTOTAL.xml")
>> how do I start the sort at 1? ít is starting at 0.

Isn't it logic that 0 comes before 1 ?

Or what do you mean ?
I know but, would like to avoid having the First ID = 0 if possible.
The only solution is to add a number (fe 999 to the items with 0) and subtract them afterwards