sort datatable and write as xml

ar_pad

Active member
Joined
Jun 5, 2006
Messages
26
Programming Experience
Beginner
Hi,

How to sort the datatable and write it as XML File.
I know that, datatable can be sorted using view and bind to datagrid and dataset1.writexml() is used to write data in XML format.
But i need to write sorted data into XML file. Meaning that, need to sort the datatable in the dataset and write dataset into xml file.

is there anyway?

Thank u!.
 
Not possible. You don't sort DataTables. There's no point anyway. Write it to XML unsorted and then when you read it again apply a Sort to the DefaultView. Whenever you bind DataTables to controls it is the contents of the DefaultView that you see anyway. If there is some unfathomable reason that you need the data sorted then you will need to Clone it, sort its DefaultView and then import the rows via the DefaultView to the clone:
VB.NET:
Dim dt2 As DataTable = dt1.Clone()

dt1.DefaultView.Sort = "Name ASC"

For Each rowView As DataRowView In dt1.DefaultView
    dt2.ImportRow(rowView.Row)
Next rowView
 
Back
Top