Binding XML file to a list control in ASP.NET
We can bind an XML file to a list control.
Here is an XML file named “books.xml”:
<?xml version=”1.0″ encoding=”ISO-8859-1″?>
<Books>
<book>
<text>book1</text>
<value>b1</value>
</book>
<book>
<text>book2</text>
<value>b2</value>
</book>
<book>
<text>book3</text>
<value>b3</value>
</book>
<book>
<text>book4</text>
<value>b4</value>
</book>
</Books>
Bind a dataset to a list control
First, include the “System.Data” namespace. We need this namespace to work with DataSet objects.
Using System.Data;
Next, create a DataSet for the XML file and load the XML file into the DataSet when the page is first loaded:
DataSet ds=New DataSet();
ds.ReadXml(Server.MapPath(”/books.xml”));
To bind the DataSet to a RadioButtonList control, first create a RadioButtonList control (without any asp:ListItem elements) in an .aspx page:
<form runat=”server”>
<asp:RadioButtonList id=”rb” runat=”server”
AutoPostBack=”True” /></form>
Then add the code that builds the XML DataSet:
DataSet ds =New DataSet();
ds.ReadXml(Server.MapPath(”/books.xml”));
rb.DataSource=ds;
rb.DataValueField=”value”;
rb.DataTextField=”text”;
rb.DataBind();
