Victor Charles
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
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
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
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
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
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.Star tupPath & "\LinkTOTAL.xml")
nodes = xdoc.SelectNodes("//LinkAI D")
For i As Integer = 0 To nodes.Count - 1
nodes(i).InnerText = i.ToString()
Next
xdoc.Save(Application.Star tupPath & "\LinkTOTAL.xml")
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.Star
nodes = xdoc.SelectNodes("//LinkAI
For i As Integer = 0 To nodes.Count - 1
nodes(i).InnerText = i.ToString()
Next
xdoc.Save(Application.Star
>> 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 ?
Isn't it logic that 0 comes before 1 ?
Or what do you mean ?
ASKER
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
also is item element is child of ID or sibling of ID?