November 30, 2005
|
Elements and transformations
|
7540 hit(s)
Since I have this-here newfangled ASP.NET 2.0 to play with, I thought I'd do something, you know, simple, just for fun. I have a list of quotations that I keep for the blog (over there on the left). At the moment, I maintain those as a text file (!); to display them, I use a stream reader to slurp them out of the text file into rows in a dataset. The show-all-quotations page uses a Repeater.
This seemed like an interesting thing to do XML-ishly. It's not that the old system had any particular flaws, but I thought I might be able to cut down on the coding by using an XmlDataSource control to read the quotations -- voilà, direct data binding! Right? Also perhaps to write out new quotes when I found them.
I read the quotes into a dataset and then used DataSet.WriteXml to create an XML file. The format looks like this:<?xml version="1.0" standalone="yes"?> <Quotations> <Quotation> <quote>A man may write at any time, if he will set himself doggedly to it.</quote> <author>Samuel Johnson</author> <source /> </Quotation> <Quotation> <quote>I have made this letter longer than usual, only because I have not had the time to make it shorter.</quote> <author>Blaise Pascal</author> <source /> </Quotation> <Quotation> <quote>Knowledge is of two kinds. We know a subject ourselves, or we know we can find information upon it.</quote> <author>Samuel Johnson</author> <source /> </Quotation> </Quotations> where <source> is for a URL, if any.
I created a new page and added an XmlDataSource control and a DataList control. And I could not get the DataList control to display anything. Nothing. Ever. I plugged into the ItemDataBound event and displayed the type of e.Item.DataItem, which reported itself as the mysterious XmlDataSourceNodeDescriptor. Sealed class, I read, and I couldn't figure out what to cast it to so I could look inside.
Eventually I was reduced to reading the docs[1], where I found this:If you have an .xml file in which property values are expressed in a format other than attributes, you can create a transformation file (.xslt) that can dynamically reformat the .xml file so that it is compatible with the XmlDataSource control. Oh, bother. Two problems. One is, you know, transformations. Blech. But transformations also have another side effect. Elsewhere in the docs it says:There are some restrictions to the editing capabilities of the XmlDataSource: - The XML data must be loaded from an XML file that is indicated by the DataFile property, not from inline XML specified in the Data property.
- No XSLT transformation can be specified in the Transform or TransformFile properties.
- ...
Dang. But I was in it this far, so I thought I might as well finish. I borrowed the transformation out of the walkthrough and adjusted it, with much tinkering, to suit my quotations file. And presto, it (eventually) worked.
I'm not sure it's worth the effort as such. I'll need to write code to update the file (although I can do that using DataSet.WriteXml again, I suppose. But it was an interesting experiment, and another example of how, if you beat your head against something long enough, you can learn something.
|