Tuesday, 25 December 2012

data fill in dropdown using xml

xml file data:
<?xml version="1.0" encoding="utf-8" ?>
<Enumerations>
  <Enums name="Campaign Activity" id="0">
    <Activity id="1">Email</Activity>
    <Activity id="2">Phone Calls</Activity>
    <Activity id="3">Fax</Activity>
    <Activity id="4">Task</Activity>
    <Activity id="5">Letter</Activity>
  </Enums>
  <Enums name="ShippingMethod" id="1">
    <Method id="1">FOB</Method>
    <Method id="2">No Charge</Method>
  </Enums>
</Enumerations>
C# code:  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        XmlDocument xmlDoc = new XmlDocument();
        string path = Server.MapPath("~/XMLFile.xml");
        xmlDoc.Load(path);
        XmlNodeList EnumNodes = xmlDoc.SelectNodes("//CRMEnumerations/Enums");
        if (!IsPostBack)
        {
            for (int i = 0; i < EnumNodes.Count; i++)
            {
                ListItem li = new ListItem();
                li.Text = EnumNodes[i].Attributes["name"].Value;
                li.Value = EnumNodes[i].Attributes["id"].Value;
                ddl.Items.Add(li);
            }
        }
     
    }
    protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddl1.Items.Clear();
        XmlDocument xmlDoc = new XmlDocument();
        string path = Server.MapPath("~/XMLFile.xml");
        xmlDoc.Load(path);
        XmlNodeList EnumNodes = xmlDoc.SelectNodes("//CRMEnumerations/Enums");
        for (int i = 0; i < EnumNodes.Count; i++)
        {
            if (EnumNodes[i].Attributes["id"].Value == ddl.SelectedValue)
            {
                for (int j = 0; j < EnumNodes[i].ChildNodes.Count; j++)
                {
                    ListItem li = new ListItem();
                    li.Text = EnumNodes[i].ChildNodes[j].InnerText;
                    li.Value = EnumNodes[i].ChildNodes[j].Attributes["id"].Value;
                    ddl1.Items.Add(li);
                }
                break;
            }
        }
    }
}

No comments:

Post a Comment