Hi – I have a pretty straight forward data binding question (from the side of writing data bindable controls) that has me confused.
Looking at the .NET source code, it seems that most data binding is abstracted via the DataBinder.GetPropertyValue() method. This method takes an object and a property name, and the method will find the value via a TypeDescriptor or reflection or whatever. This works great on an IListSource such as DataSet, however I can’t get it working with an XmlNode object. I know that you can bind an XmlNodeList to a data source in ASP.NET, so I would expect this to work. Here’s the code:
class Program
{
static void Main(string[] args)
{
XmlDocument doc = new XmlDocument();
doc.Load("Data.xml");
IEnumerable list = doc.SelectNodes("/Data/Row");
foreach (object item in list)
{
object val = DataBinder.GetPropertyValue(item, "Number"); //Expect to see “1”, “2” and “3”
}
}
}
And Data.xml is:
<?xml version="1.0" encoding="utf-8" ?>
<Data>
<Row Number="1" />
<Row Number="2" />
<Row Number="3" />
</Data>
When I call GetPropertyValue, I get this exception:
DataBinding: 'System.Xml.XmlElement' does not contain a property with the name 'Number'.
In my Data Binding loop, I just want to loop through any IEnumerable – I don’t want to special case the XmlNode type. Controls such as DropdownList will special case IListSource and do some conversions, however other IEnumerables look like they’re treated as is. Thanks!