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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting with the XMLdatasource control

Status
Not open for further replies.

barrylowe

Programmer
Nov 6, 2001
188
0
0
GB
I have a gridview which is bound to an XMLdatasource control.

Everything is working perfectly apart from the sorting functionality. I am aware that sorting is not supported by the XMLdatasource.

Can anyone tell me the easiest way around this problem?
 
don't use datasource controls. as you can see they are very limiting. I would load the xml file into an xml document. I would then parse the xml document into full objects. add these objects to a list. then you can search/sort the list and bind the list to the grid.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Yeah, I was trying to utilise the built-in functionality that the XMLdatasource provides and to be honest with the exception of the sorting it has worked a treat.

I actually have an XMLDocument object loaded in that page which I use for other little pieces of dynamic coding, however I am still using the XMLDatasource for the grid binding.

Can you explain what you mean by parsing the Document into full ojects?
 
How about loading the XML in to a dataset and use dataviews to sort your list?
 
Can you explain what you mean by parsing the Document into full ojects?
map nodes/attributes to Pocos (plain old compiled objects)
Code:
public class Foo
{
   public int Number {get;set;}
   public string Text {get;set;}
   public DateTime Date {get;set;}
}

public class Mapper
{
   public Foo MapFrom(XmlNode node)
   {
      Foo foo = new Foo();
      foo.Number = int.Parse(node.Attributes["id"]);
      foo.Text = node.Value;
      foo.Date = DateTime.Parse(node.Attributes["TimeStamp");]
   } 
}

public class FooService
{
     public List<Foo> GetAll(IComparer<Foo> sorting)
     {
         List<Foo> foos = new List<Foo>();
         foreach(XmlNode node in xmlDoc.Nodes)
         {
             foos.Add(mapper.MapFrom(node);
         }
         foos.Sort(sorting);
         return foos;
     }
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top