Web Forms XML files
ASP.NET Web Forms - XML File
We can bind XML files to list controls.
An XML file
There is an XML file named "countries.xml":
<countries>
<country>
<text>Norway</text>
<value>N</value>
</country>
<country>
<text>Sweden</text>
<value>S</value>
</country>
<country>
<text>France</text>
<value>F</value>
</country>
<country>
<text>Italy</text>
<value>I</value>
</country>
</countries>
View this XML file: countries.xml
Bind DataSet to List control
First, import the "System.Data" namespace. We need this namespace to work with DataSet objects. Include the following directive at the top of the .aspx page:
Next, for XML File creates a DataSet, and loads this XML file into the DataSet when the page is first loaded:
sub Page_Load
if Not Page.IsPostBack then
dim mycountries=New DataSet
mycountries.ReadXml(MapPath("countries.xml"))
end if
end sub
#In order to bind data to the RadioButtonList control, first create a RadioButtonList in the .aspx page Control (without any asp:ListItem elements):
<body>
<form runat="server">
<asp:RadioButtonList id="rb" runat="server" AutoPostBack="True" />
</form>
</body>
</html>
Then add the script to create the XML DataSet and bind the values in the XML DataSet to the RadioButtonList control:
<script runat="server">
sub Page_Load
if Not Page.IsPostBack then
dim mycountries=New DataSet
mycountries.ReadXml(MapPath("countries.xml"))
rb.DataSource=mycountries
rb.DataValueField="value"
rb.DataTextField="text"
rb.DataBind()
end if
end sub
</script>
<html>
<body>
<form runat ="server">
<asp:RadioButtonList id="rb" runat="server"
AutoPostBack="True" onSelectedIndexChanged="displayMessage" />
</form>
</body>
</html>
Then we add a subroutine that when the user clicks an item in the RadioButtonList control, the subroutine will be executed. When a radio button is clicked, a line of text will appear in the label:
Instance
<%@ Import Namespace="System.Data" %> <script runat="server"> sub Page_Load if Not Page.IsPostBack then dim mycountries=New DataSet mycountries.ReadXml(MapPath("countries.xml")) rb.DataSource=mycountries rb.DataValueField="value" rb.DataTextField="text" rb.DataBind() end if end sub sub displayMessage(s as Object,e As EventArgs) lbl1.text="Your favorite country is: " & rb.SelectedItem.Text end sub </script> <!DOCTYPE html> <html> <body> <form runat="server"> <asp:RadioButtonList id="rb" runat="server" AutoPostBack="True" onSelectedIndexChanged="displayMessage" /> <p><asp:label id="lbl1" runat="server" /></p> </form> </body> </html>
Run Example»
Click the "Run Instance" button to view the online instance