Dave Burke - Freelance .NET Developer specializing in Online Communities

A freelance .NET Developer

Economy with XML and DataRows

I do not often write a block of code and sit for a few moments to admire it before moving onto the next task, but the economy of using XML with ADO.NET DataRows to generate a list from two different XML files was too sweet to disregard.  In 15 minutes and 12 lines of code I was able to display a dynamically generated item list from an XML file based on a record ID passed to it during an OnItemDataBound event.

I've done similar tasks before with dataview filtering.  This resulted in more lines of code and wasn't nearly as efficient as using the datarow[] from a datatable.select.

  private string MenuSubItems(int plid)
  {

   DataSet ds = new DataSet();

   ds.ReadXmlSchema(Server.MapPath("/utils/xml/subplace.xsd"));
   ds.ReadXml(Server.MapPath("/utils/xml/subplace.xml"), XmlReadMode.IgnoreSchema);
   ds.Tables[0].TableName = "subplaces";
   DataRow[] dtrows = ds.Tables[0].Select("plid = " + plid.ToString(),"pltitle asc");

   string s = "";
   for (int i = 0; i < dtrows.Length; i++)
   {
     s += dtrows[i]["pltitle"].ToString() + "\n";
   }
   return s;
  }

Posted: Jan 22 2004, 10:51 PM by daveburke | with 4 comment(s)
Filed under:

Comments

Vijay said:

Dave, the DataView apart from being a bit cumbersome to code, is really heavy in terms of memory consumption. I am not sure whether it is exactly a materialized view over the DataTable kind of thing but it just hogs memory. The DataRow[] got by the Select method returns references of the Rows in the DataTable and you can imagine how fast that will be. There is no object allocation, creation and then disposing stuff ... This is one thing i learnt recently in a project i worked on .. will post about this later !
# January 23, 2004 2:53 AM

TrackBack said:

Economy with XML and DataRows I do not often write a block of code and sit for a few moments to admire it before moving onto the next task, but the economy of using XML with ADO.NET&nbsp;DataRows to generate a...
# January 23, 2004 11:45 PM

Daniel Turini said:

Not to mention that you could speed it up a bit by using a StringBuilder...
# January 28, 2004 10:06 AM

Dave Burke said:

Ha. That's right, Daniel. Thanks for the reminder!
# January 28, 2004 10:18 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)