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

Introduction: In previous articles i explained How to get CheckBoxList selected items in comma separated formatand Example to validate asp.net CheckBoxList using jQuery and How to validate CheckBoxList using JavaScript and Create,Read,Sort Xml file and Bind to RadioButtonList and Create,Read,Sort Xml file and Bind to ListBox and Read and Bind DropDownList from XML file data. 

Description: In this article I have explained how to Create, Read, Sort XML file and Bind / Load / Fill to  CheckBoxList control in asp.net. CheckBoxList is binded in the sorted order of Skill Name In this example I have used Xml file as a data source to bind with checkboxlist. You can also  read my article “How to Fill CheckBoxList from Sql Server table in asp.net(C#,VB) If you are interested in binding CheckBoxList with Sql server database. 

read and bind asp.net checkboxlist from xml file
Implementation: Let's create an asp.net sample web application to check it out.
  • 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 “Skills.xml”. Then paste the following xml tags in the Skills.xml file

<Skills>
  <Skill>
    <Id>1</Id>
    <Name>Asp.Net</Name>
  </Skill>
  <Skill>
    <Id>2</Id>
    <Name>jQuery</Name>
    <Skill>
      <Id>3</Id>
      <Name>JavaScript</Name>
    </Skill>
    <Skill>
      <Id>4</Id>
      <Name>WCF</Name>
    </Skill>
    <Skill>
      <Id>5</Id>
      <Name>Sql Sever</Name>
    </Skill>   
    <Skill>
      <Id>6</Id>
      <Name>XML</Name>
    </Skill> 
</Skills>
  •  Now in the design page (.aspx) Place a CheckBoxList control from the visual studio toolbar and set its properties as:
<fieldset style="width:280px;">
    <legend>Bind CheckBoxList from Xml File in asp.net</legend>
     Select Skills : <asp:CheckBoxList ID="cbSkills" runat="server" RepeatColumns="3"
            RepeatDirection="Horizontal">
        </asp:CheckBoxList>
        <asp:Label ID="lblStatus" runat="server" ForeColor="#009933"></asp:Label>
    </fieldset>


C#.Net Code to Create, Read, Sort and Bind asp.net CheckBoxList 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)
        {
            BindCheckBoxListFromXML();
        }
    }

    public void BindCheckBoxListFromXML()
    {
        DataSet ds = new DataSet();
        try
        {
            //Reading the data from  the XML file
            ds.ReadXml(MapPath("~/Skills.xml"));
            DataView dv = ds.Tables[0].DefaultView;
            //Sort the DataView by "Name" 
            dv.Sort = "Name";
            //Setting DataText field and DataValue field of CheckBoxList 
            cbSkills.DataTextField = "Name";
            cbSkills.DataValueField = "Id";
            //Binding the CheckBoxList with the  DataView 
            cbSkills.DataSource = dv;
            cbSkills.DataBind();
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message.ToString());
        }
    }

VB.Net Code to Create, Read,Sort and Bind asp.net CheckBoxList control from Xml file data

  • 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
            BindCheckBoxListFromXML()
        End If
    End Sub

    Public Sub BindCheckBoxListFromXML()
        Dim ds As New DataSet()
        Try
            'Reading the data from  the XML file
            ds.ReadXml(MapPath("~/Skills.xml"))
            Dim dv As DataView = ds.Tables(0).DefaultView
            'Sort the DataView by "Name" 
            dv.Sort = "Name"
            'Setting DataText field and DataValue field of CheckBoxList 
            cbSkills.DataTextField = "Name"
            cbSkills.DataValueField = "Id"
            'Binding the CheckBoxList with the  DataView 
            cbSkills.DataSource = dv
            cbSkills.DataBind()
        Catch ex As Exception
            Response.Write(ex.Message.ToString())
        End Try
    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..