Google How to Create,Read,Sort Xml file and Bind to ListBox in asp.net | Asp.Net,C#.Net,VB.Net,MVC,jQuery,JavaScipt,AJAX,WCF,Sql Server example

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

Introduction: In previous articles i explained How to Create,Read,Sort Xml file and Bind to RadioButtonList and How to Create,Read,Sort Xml file and Bind to CheckBoxList  and How to Read and Bind DropDownList from XML file data and Count number of times website visited and online users and Bind,Save and perform run time calculations in Repeater control and Retain password value in the asp.net TextBox after postback and Bind,Save,Edit,Update,Cancel,Delete and paging example in DetailsView and and
In this article I have explained How to Create,Read,Sort Xml  file and Bind to ListBox in  asp.net. ListBox will be binded after sorting by Qualification Name. In this example I have used Xml file as a data source to bind with ListBox. 



Implementation: Let's create an 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>

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

<fieldset style="width:350px;">
    <legend>Bind ListBox from XML file data in asp.net</legend>  
    <table width="100%">
    <tr>
    <td>Select Qualifiation : </td>
    <td><asp:ListBox ID="lsbQualification" runat="server" AutoPostBack="True"
            onselectedindexchanged="lsbQualification_SelectedIndexChanged"
            Height="100px" Width="95px"></asp:ListBox></td>
    </tr>
    <tr><td colspan="2"><asp:Label ID="lblStatus" runat="server" Text="" style="color: #009933"></asp:Label></td></tr>
    </table>    
         </fieldset>


C#.Net Code to Create,Read,Sort Xml file data and Bind to ListBox in asp.net
  •  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)
        {
            BindListBoxFromXML();
        }
    }
    public void BindListBoxFromXML()
    {
        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 ListBox 
            lsbQualification.DataTextField = "Name";
            lsbQualification.DataValueField = "ID";
            //Binding the ListBox with the  DataView 
            lsbQualification.DataSource = dv;
            lsbQualification.DataBind();
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message.ToString());
        }
    }
    protected void lsbQualification_SelectedIndexChanged(object sender, EventArgs e)
    {
        lblStatus.Text = "Selected Qualification: " + lsbQualification.SelectedItem.Text + " & " + "Selected Value : " + lsbQualification.SelectedValue;
    }

VB.Net Code  to Create,Read,Sort Xml file data and Bind to ListBox in asp.net
  • In the design page (.aspx) Place a ListBox control and a Label control from the visual studio toolbar and set their properties as:
 
<fieldset style="width:350px;">
    <legend>Bind ListBox from XML file data in asp.net</legend>  
    <table width="100%">
    <tr>
    <td>Select Qualifiation : </td>
    <td><asp:ListBox ID="lsbQualification" runat="server" AutoPostBack="True"
            onselectedindexchanged="lsbQualification_SelectedIndexChanged"
            Height="100px" Width="95px"></asp:ListBox></td>
    </tr>
    <tr><td colspan="2"><asp:Label ID="lblStatus" runat="server" Text="" style="color: #009933"></asp:Label></td></tr>
    </table>    
         </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
            BindListBoxFromXML()
        End If
    End Sub

    Public Sub BindListBoxFromXML()
        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 ListBox 
            lsbQualification.DataTextField = "Name"
            lsbQualification.DataValueField = "ID"
            'Binding the ListBox with the  DataView 
            lsbQualification.DataSource = dv
            lsbQualification.DataBind()
        Catch ex As Exception
            Response.Write(ex.Message.ToString())
        End Try
    End Sub

    Protected Sub lsbQualification_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lsbQualification.SelectedIndexChanged
        lblStatus.Text = ("Selected Qualification: " + lsbQualification.SelectedItem.Text & " & " & "Selected Value : ") + lsbQualification.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."

No comments :

Post a Comment

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..