ASP.NET Web Forms - XML file
exist In ASP.NET, you can bind an XML file to a List control by treating it as a data source. Please refer to this section.We can bind the XML file to the list control.
an XML file
Here is an XML file called "countries.xml":
<?xml version="1.0" encoding="ISO-8859-1"?><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>Check out 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 your .aspx page:
<%@ Import Namespace="System.Data" %>Next, create a DataSet for the XML file and load the XML file into the DataSet when the page first loads:
<script runat="server">sub Page_Loadif Not Page.IsPostBack thendim mycountries=New DataSetmycountries.ReadXml(MapPath("countries.xml"))end ifend subIn order to bind data to the RadioButtonList control, first create a RadioButtonList control in the .aspx page (without any asp:ListItem elements):
<html><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:
<%@ Import Namespace="System.Data" %><script runat="server">sub Page_Loadif Not Page.IsPostBack thendim mycountries=New DataSetmycountries.ReadXml(MapPath("countries.xml"))rb.DataSource=mycountriesrb. DataValueField="value"rb.DataTextField="text"rb.DataBind()end ifend 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 will be executed when the user clicks an item in the RadioButtonList control. When a radio button is clicked, a line of text will appear in the label:
Example
<%@ Import Namespace="System.Data" %><script runat="server">sub Page_Loadif Not Page.IsPostBack thendim mycountries=New DataSetmycountries.ReadXml(MapPath("countries.xml"))rb.DataSource=mycountriesrb. DataValueField="value"rb.DataTextField="text"rb.DataBind()end ifend subsub displayMessage(s as Object,e As EventArgs)lbl1.text="Your favorite country is: " & rb.SelectedItem.Textend sub</script><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>The above is an introduction to ASP.NET XML data binding.