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;
}