How XML Bind with Dropdownlist in asp.net c#

In this article, I will bind the drop down list with xml file. Many time we need to bind the data with front side means client side so it’s the best way (xml file) to bind with any control because it is also client side mark up language,


 First we need to just create the xml file like below:-
<?xml version="1.0" encoding="utf-8" ?>
<EmployeeName>
  <empname>
    <id>1</id>
    <name>Neeraj Srivastava</name>
  </empname>
  <empname>
    <id>2</id>
    <name>Sanjay kumar</name>
  </empname>
  <empname>
    <id>3</id>
    <name>Arun Kumar</name>
  </empname>
  <empname>
    <id>4</id>
    <name>Meenakshi Pandey</name>
  </empname>
</EmployeeName>

Source Code:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="bindxml.aspx.cs" Inherits="bindxml" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>   
    <asp:DropDownList ID="ddlempname" runat="server">
        </asp:DropDownList>
    </div>
    </form>
</body>
</html>

Code behind (c#):-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class bindxml : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        BindddlXml();
    }
    private void BindddlXml()
    {
       
    
        using (DataSet ds = new DataSet())
        {
           
            ds.ReadXml(Server.MapPath("~/Empname.xml"));
            ddlempname.DataSource = ds;
            ddlempname.DataTextField = "name";
            ddlempname.DataValueField = "id";
            ddlempname.DataBind();

        }
    }
}

OutPut:-




How XML Bind with Dropdownlist in asp.net c# How XML Bind with Dropdownlist in asp.net c# Reviewed by NEERAJ SRIVASTAVA on 10:01:00 AM Rating: 5

No comments:

Powered by Blogger.