Bind an XML file to a dropdownlist



Question 2:
The XML file below has a list of employees. Your job is to bind the employee IDs and Names to a dropdownlist. ID must be dropdownlist value field and name must be the dropdownlist Text field. Also, only the active employees must be binded to the dropdownlist and the names should be in the ascending order. When I select a name from the dropdownlist, the name and ID of the selected employee must be printed on the webform.

Employees.xml
<Employees>
<Employee>
<Name>David</Name>
<ID>101</ID>
<IsActive>true</IsActive>
</Employee>
<Employee>
<Name>Tom</Name>
<ID>102</ID>
<IsActive>true</IsActive>
</Employee>
<Employee>
<Name>Rick</Name>
<ID>103</ID>
<IsActive>false</IsActive>
</Employee>
<Employee>
<Name>Mark</Name>
<ID>104</ID>
<IsActive>true</IsActive>
</Employee>
</Employees>

Code sample:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataSet DS = new DataSet();
DS.ReadXml(Server.MapPath("Employees.xml"));

DataView DV = DS.Tables["Employee"].DefaultView;
DV.RowFilter = "IsActive='true'";
DV.Sort = "Name asc";

DropDownList1.DataSource = DV;
DropDownList1.DataValueField = "ID";
DropDownList1.DataTextField = "Name";
DropDownList1.DataBind();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Write("Name Is : " + DropDownList1.SelectedItem.Text + " and ID is " + DropDownList1.SelectedItem.Value);
}

Code Explanation:
1.
Read the XML data from Employees.xml file into a DataSet. We make use of the ReadXml() method. ReadXml method loads the XML data into the dataset DS. DS.ReadXml(Server.MapPath("Employees.xml"));

2. Now you have the Data in a relational format in the dataset. Create a DataView on the employees table in the DataSet. The DefaultView property of DataTable returns the DataView.
DataView DV = DS.Tables["Employee"].DefaultView;

3. After you have created the DataView, apply the RowFilter, to select only the active employees. You apply the RowFilter as shown below.
DV.RowFilter = "IsActive='true'";

4. Now sort the data in the DataView in ascending order. We sort the data on the Name column. You can apply the sort expression on a dataview as shown below.
DV.Sort = "Name asc";

5. Finally set the DataSource, DataValueField and DataTextField properties of the dropdownlist and call the DataBind() method as shown in the below code.
DropDownList1.DataSource = DV;
DropDownList1.DataValueField = "ID";
DropDownList1.DataTextField = "Name";
DropDownList1.DataBind();


Untill now we have seen how to bind an XML file to dropdownlist. We have also seen how to create a DataView on DataTable. DataView is used for sorting and filtering the data. Now we have to get the SelecteValue and SelectedItem Text of a dropdownlist. To achieve this, follow the below steps.

1. Set the autopostback property of the dropdownlist to true. So, when ever a selection in the dropdownlist changes, the webform is posted back to the server automatically.

2. In the DropDownList1_SelectedIndexChanged event handler we can capture the employee name and id using the DropDownList1.SelectedItem.Text and DropDownList1.SelectedItem.Value properties as shown below.

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Write("Name Is : " + DropDownList1.SelectedItem.Text + " and ID is " + DropDownList1.SelectedItem.Value);
}

Note: If you have noticed, we binded the XML file to the dropdownlist only during the initial page load and not during every post back. We do this check by using if(!IsPostBack) property of the page.

6 comments:

  1. Excellent Venkat!!

    ReplyDelete
  2. write a program to connect the database using ado object model to update multiple records...

    this is a question asked by a company on machine test...

    simple program but your explanation on this question might help the beginners!!!

    ReplyDelete
  3. Nice work. It helps me so much. Thanksssssss a lot.

    ReplyDelete
  4. this is to much helpful for me...

    ReplyDelete
  5. DataSet ds = new DataSet();
    ds.ReadXml("c:\\emp.xml");

    List empList = (from emp in ds.Tables[0].AsEnumerable()
    where emp.Field("IsActive") == "true"
    orderby emp.Field("Name")
    select new Emp
    {
    ID = Convert.ToInt32( emp.Field("ID") ),
    Name = emp.Field("Name"),
    IsActive = ( emp.Field("IsActive"))
    }).ToList();


    MyDDL.DataSource = empList;
    MyDDL.DataTextField = "Name";
    MyDDL.DataValueField = "ID";
    MyDDL.DataBind();

    ReplyDelete
  6. XmlDocument xml = new XmlDocument();
    xml.LoadXml(myXmlString);

    XmlNodeList xnList = xml.SelectNodes("/Employees/Employee");

    foreach (XmlNode xn in xnList)
    {
    if (xn["IsActive"].InnerText == "true")
    {
    string Name = xn["Name"].InnerText;
    string ID = xn["ID"].InnerText;

    ddlEmp.Items.Add(new ListItem(Name, ID));

    }

    }

    ReplyDelete

If you are aware of any other asp.net questions asked in an interview, please post them below. If you find anything missing or wrong, please feel free to correct by submitting the form below.

 
Disclaimer - Terms of use - Contact Us