Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to sort a XML file?

Status
Not open for further replies.

kre1973

IS-IT--Management
May 5, 2006
47
US
I heard of importing your xml doc into a dataset, then sort the dataset and re-populate your xml, but I'm not sure how to code this..
I'd like to sort it by the 'Name' node.
Here is my XML file:
Code:
<?xml version="1.0"?>
<IndividualListing>
  <Individual>
    <Name>Aaron, Scott</Name>
    <Pager>3122220365@archwireless.net</Pager>
  </Individual>
  <Individual>
    <Name>Boston, Jeff</Name>
    <Pager>6152257514@archwireless.net</Pager>
  </Individual>
  <Individual>
    <Name>Adams, Ryan</Name>
    <Pager>2129410781@archwireless.net</Pager>
  </Individual>
  <Individual>
    <Name>Thomas, Naveed</Name>
    <Pager>6159416077@archwireless.net</Pager>
  </Individual>
  <Individual>
    <Name>Baker, Srinivas</Name>
    <Pager>3122010073@archwireless.net</Pager>
  </Individual>
  <Individual>
    <Name>Wilson, Don</Name>
    <Pager>8472222016@archwireless.net</Pager>
  </Individual>
  <Individual>
    <Name>Naveed Reddy</Name>
    <Pager>3122225858@archwireless.net</Pager>
  </Individual>
  <Individual>
    <Name>Sanders, William</Name>
    <Pager>3122224506@archwireless.net</Pager>
  </Individual>
  <Individual>
    <Name>Anderson, Karl R.</Name>
    <Pager>3122051023@archwireless.net</Pager>
  </Individual>
</IndividualListing>

thanks
 
Well, there are two ways to do this. If you are using .NET 2.0, you can do this:

Dim dsOriginalXML As DataSet
Dim dsSortedXML As DataSet
Dim dr As DataRow

dsOriginalXML = New DataSet

dsOriginalXML.ReadXml("C:\Temp\test.xml")

dsOriginalXML.Tables(0).DefaultView.Sort = "Name"

dsSortedXML = New DataSet

dsSortedXML.Tables.Add(dsOriginalXML.Tables(0).DefaultView.ToTable())

dsOriginalXML.Dispose()
dsOriginalXML = Nothing

dsSortedXML.WriteXml("C:\Temp\test.xml")


In .NET 1.x, there is no ToTable method of the DataView object, so you need to do this "manually":


Dim dsOriginalXML As DataSet
Dim dsSortedXML As DataSet
Dim dr As DataRow

dsOriginalXML = New DataSet

dsOriginalXML.ReadXml("C:\Temp\test.xml")

dsOriginalXML.Tables(0).DefaultView.Sort = "Name"

dsSortedXML = New DataSet

dsSortedXML.Tables.Add(dsOriginalXML.Tables(0).Clone)

For Each drv As DataRowView In dsOriginalXML.Tables(0).DefaultView

dr = dsSortedXML.Tables(0).NewRow

For Each dc As DataColumn In dsSortedXML.Tables(0).Columns
dr.Item(dc.ColumnName) = drv.Item(dc.ColumnName)
Next

dsSortedXML.Tables(0).Rows.Add(dr)

dr = Nothing
Next

dsOriginalXML.Dispose()
dsOriginalXML = Nothing

dsSortedXML.WriteXml("C:\Temp\test.xml")

Post back if you have any questions.


Before I finish, there are 2 questions/issues I would like to raise.

1) Why do you need to save the data sorted. One of the main points of databases - including XML - is that they do NOT need to be stored in a sorted state...they can be sorted and resorted as needed, on the fly.

2) I hope the sample data you posted is actually sample data. I hope you didn't post people's actual names and pager numbers on the interwebs for the whole world to see.



I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Now that would be funny... :-D

Would be even more funny if they use it also as their login...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top