Click here to Skip to main content
15,885,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello every one

I am working with xml files. I did work with xml before this today i stuck at one point, I know how to save update and delete in xml file But i dnt know how to do sorting in xml and save that sort result in a dataset so that in can use that dataset in a data control. Please tell me if anybody did that how to do sorting in ascending or descending order.
Posted

Please check like this
btnReader_Click show the original result of Xml file
btnArrange_Click show the descending order





protected void btnReader_Click(object sender, EventArgs e)
{
string myXMLfile = @"E:\jlpt3.xml";
DataSet ds = new DataSet();
// Create new FileStream with which to read the schema.
System.IO.FileStream fsReadXml = new System.IO.FileStream
(myXMLfile, System.IO.FileMode.Open);

ds.ReadXml(fsReadXml);
GridView1.DataSource = ds;
GridView1.DataBind();


fsReadXml.Close();


}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
protected void btnArrange_Click(object sender, EventArgs e)
{
string myXMLfile = @"E:\jlpt3.xml";
DataSet ds = new DataSet();
System.IO.FileStream fsReadXml = new System.IO.FileStream
(myXMLfile, System.IO.FileMode.Open);

ds.ReadXml(fsReadXml);
DataView dv = new DataView(ds.Tables[0]);
DataTable dt = dv.Table;

dt.Columns[0].ColumnName = "kana";
dt.Columns[1].ColumnName = "kanji";
dt.Columns[2].ColumnName = "type";
dt.Columns[3].ColumnName = "english";

dv.Sort = dt.Columns[3].ColumnName;

// append "DESC" to the sort field name in order to
// sort descending
dv.Sort += " DESC";


GridView1.DataSource = dv;
GridView1.DataBind();


fsReadXml.Close();


}
 
Share this answer
 
Comments
[no name] 21-Jun-13 2:13am    
Let me to learn back again if i would make mistake
:)
AZAD CHOUHAN 21-Jun-13 2:31am    
thanks its work now :)
AZAD CHOUHAN 21-Jun-13 2:33am    
this thing work for me :)

String File = Server.MapPath("~/Data/BlogContent.xml");
DataSet ds = new DataSet();

// FileStream fsread = new FileStream(File, FileMode.Open);
ds.ReadXml(File);
DataView dv = new DataView(ds.Tables[0]);
DataTable dt = dv.Table;
dv.Sort = dt.Columns["id"].ColumnName;
dv.Sort += " Asc";
// ds.Tables[0].DefaultView.Sort= "id desc";


Repeater1.DataSource = dv;
Repeater1.DataBind();
AZAD CHOUHAN 21-Jun-13 2:42am    
Hey now one more problem have to solve Now it is sorting succesfully now i wanna to get only top 5 records from xml
Hi ,,

First read the XML file in to dataset
DataSet ds=new DataSet();
 ds.ReadXml("c:\\Test.xml");
Sort the DataSet

ds.Tables[0].DefaultView.Sort ="EmpID"; 

Hope this helps you.

Thanks
--RA
 
Share this answer
 
Comments
AZAD CHOUHAN 21-Jun-13 1:18am    
i wanna to sort in asc order and desc order how can i do that?
Rajesh Anuhya 21-Jun-13 1:22am    
You can use the above syntex with "asc,desc"
for example
ds.Tables[0].DefaultView.Sort ="EmpID asc";
ds.Tables[0].DefaultView.Sort ="EmpID desc";
--RA
AZAD CHOUHAN 21-Jun-13 1:29am    
AZAD CHOUHAN - 2 mins ago
not working i use this String File = Server.MapPath("~/Data/BlogContent.xml"); DataSet ds = new DataSet(); ds.ReadXml(File); ds.Tables[0].DefaultView.Sort= "id asc"; Repeater1.DataSource = ds; Repeater1.DataBind();
Rajesh Anuhya 21-Jun-13 1:31am    
??????
AZAD CHOUHAN 21-Jun-13 1:42am    
yess sir asc and desc is not sorting my data :(

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900