Wednesday, November 30, 2022
Code Search
Code Bank
.NET Applications
XSD Schema Generator
.NET/XML Training


ASP.NET DropDownList XML Binder Class

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.

Teaches ASP.NET, XmlTextReader class, C#
Requirements .NET 1.0
Example: Click Here
View Source Code: Click Here
Download Code: Click Here


.NET, SharePoint and Silverlight Training Solutions
    Online, Onsite and Video Training on .NET and SharePoint technologies available through The Wahlin Group.

© 2007 Wahlin Consulting LLC
XML for ASP.NET Developers in bookstores