There may be many occasions when a complex hierarchical
XML document needs to be bound to an DropDownList control in an
ASP.NET page. Although there are different ways to approach this, this
sample introduces a class named XmlReaderBinder that has a single
static method named BindDropDown(). This method accepts the name
of the element containing the data that should be bound, the attribute
name(s) for the value and text data to be used in creating
a ListItem control, and the path to the source XML Document. If
the XML data is not contained in the target element"s attributes but is
instead in child nodes, the control will automatically find these child
nodes and extract their text node values instead.
An example of using this class to bind an XML document
named "lookup.xml" to a DropDownList control named ddStates
in an ASP.NET page is shown below:
string xmlPath = Server.MapPath("lookup.xml");
XmlReaderBinder.BindDropDown(this.ddStates,"state","val",
"text",xmlPath);
To see the class in action or view the source code click
one of the links below.
|