How to Create,Read,Sort Xml file and Bind to RadioButtonList in asp.net

Introduction:  In previous articles i explained How to Create,Read,Sort Xml file and Bind to CheckBoxList and Create,Read,Sort Xml file and Bind to DropDownList and Create,Read,Sort Xml file and Bind to ListBox and Validate DropDownList using jQuery and  Send email to multiple users based on CheckBox selection inside GridView.

Description: In this article I have explained how to Create, Read, Sort XML file and Bind/Load/Fill RadioButtonList  control in asp.net. Binded RadioButtonList is also sorted by Qualification Name. In this example I have used Xml file as a data source to bind with RadioButtonList . You can also read my article “How to Bind RadioButtonList from Sql server table in asp.net” If you are interested in binding RadioButtonList  with Sql server database. 

read and bind asp.net radiobuttonlist from xml file

Implementation: Let's create an asp.net example web application to see it in action.
  •  Create a new website and add XML file. To add XML file follow the simple steps:
Go to Website Menu -> Add New Item -> Select XML File and give name “Qualifications.xml”. Then paste the following xml tags in the Qualifications.xml file

<Qualifications>
  <Qualification>
    <Id>1</Id>
    <Name>MCA</Name>
  </Qualification>
  <Qualification>
    <Id>2</Id>
    <Name>BCA</Name>
    <Qualification>
      <Id>3</Id>
      <Name>M.Tech</Name>
    </Qualification>
    <Qualification>
      <Id>4</Id>
      <Name>B.tech</Name>
    </Qualification>
    <Qualification>
      <Id>5</Id>
      <Name>M.Sc</Name>
    </Qualification>
    <Qualification>
      <Id>6</Id>
      <Name>B.Sc</Name>
    </Qualification>
</Qualifications>

Save the file.
  •  Now in the design page (.aspx) Place a RadioButtonList control and a Label control from the visual studio toolbar and set their properties as:

<fieldset style="width:320px;">
    <legend>Bind RadioButtonList from XML file data in asp.net</legend>  
        Select Qualifiation :
        <asp:RadioButtonList ID="rblQualification" runat="server" RepeatColumns="3"
            RepeatDirection="Horizontal" AutoPostBack="true"
            onselectedindexchanged="rblQualification_SelectedIndexChanged">
        </asp:RadioButtonList>
         <asp:Label ID="lblStatus" runat="server" Text="" style="color: #009933"></asp:Label>
         </fieldset>


C#.Net Code to Create, Read, Sort and Bind asp.net RadioButtonList control from Xml file data
  •  In the code behind file (.aspx.cs) add namespace :
using System.Data;

then write the code as:
protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindRadioButtonListFromXML();
        }
    }
    public void BindRadioButtonListFromXML()
    {
        DataSet ds = new DataSet();
        try
        {
            //Reading the data from  the XML file
            ds.ReadXml(MapPath("~/Qualifications.xml"));
            DataView dv = ds.Tables[0].DefaultView;
            //Sort the DataView by "Name" 
            dv.Sort = "Name";
            //Setting DataText field and DataValue field of RadioButtonList 
            rblQualification.DataTextField = "Name";
            rblQualification.DataValueField = "ID";
            //Binding the RadioButtonList with the  DataView 
            rblQualification.DataSource = dv;
            rblQualification.DataBind();          
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message.ToString());
        }
    }
    protected void rblQualification_SelectedIndexChanged(object sender, EventArgs e)
    {
        lblStatus.Text = "Selected Qualification: " + rblQualification.SelectedItem.Text + " & " + "Selected Value : " + rblQualification.SelectedValue;
    }

VB.Net Code to Create, Read, Sort and Bind asp.net RadioButtonList control from Xml file data
  • In the design page (.aspx) Place a RadioButtonList control and a Label control from the visual studio toolbar and set their properties as:

<fieldset style="width:320px;">
    <legend>Bind RadioButtonList from XML file data in asp.net</legend>  
        Select Qualifiation :
        <asp:RadioButtonList ID="rblQualification" runat="server" RepeatColumns="3"
            RepeatDirection="Horizontal" AutoPostBack="true">
        </asp:RadioButtonList>
         <asp:Label ID="lblStatus" runat="server" Text="" style="color: #009933"></asp:Label>
         </fieldset>

  • In the code behind file (.aspx.vb) add namespace :
Imports System.Data

Then write the code as:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            BindRadioButtonListFromXML()
        End If
    End Sub

    Public Sub BindRadioButtonListFromXML()
        Dim ds As New DataSet()
        Try
            'Reading the data from  the XML file
            ds.ReadXml(MapPath("~/Qualifications.xml"))
            Dim dv As DataView = ds.Tables(0).DefaultView
            'Sort the DataView by "Name" 
            dv.Sort = "Name"
            'Setting DataText field and DataValue field of RadioButtonList 
            rblQualification.DataTextField = "Name"
            rblQualification.DataValueField = "ID"
            'Binding the RadioButtonList with the  DataView 
            rblQualification.DataSource = dv
            rblQualification.DataBind()
        Catch ex As Exception
            Response.Write(ex.Message.ToString())
        End Try
    End Sub

    Protected Sub rblQualification_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rblQualification.SelectedIndexChanged
        lblStatus.Text = ("Selected Qualification: " + rblQualification.SelectedItem.Text & " & " & "Selected Value : ") + rblQualification.SelectedValue
    End Sub

Now over to you:
"If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linked in and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates."
Previous
Next Post »

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..