How to sort Name by asc from XML file and populate the dropdown?



Hi
This is the frequently asked question in interview. “Write a program to sort the Empname by asc from Xml file and populate the dropdown”.

We can do using 2 approach
1. using dataset and dataview concept
2. using Linq to Xml concept

Here i m writting code using dataset and dataview concept
Step1: Create some Xml file like this

<?xml version="1.0" encoding="utf-8" ?>
<Emps>
<Emp>
<Id>1</Id>
<Name>Ram</Name>
<Address>xBangalore</Address>
</Emp>
<Emp>
<Id>2</Id>
<Name>RamMohan</Name>
<Address>Chenai</Address>
</Emp>
<Emp>
<Id>3</Id>
<Name>Ram</Name>
<Address>Nepal</Address>
</Emp>
</Emps>

Step 2: Write the C# code in codebehind file like this

protected void Page_Load(object sender, EventArgs e)
{

string filepath = Server.MapPath("../Emp.xml");
using (DataSet ds = new DataSet())
{
ds.ReadXml(filepath);
DataView dv = new DataView();
dv =ds.Tables["Emp"].DefaultView;
dv.RowFilter = "Name='Ram'";
dv.Sort = "Address Asc";
DropDownList1.DataSource = dv;
DropDownList1.DataTextField = "Address";
DropDownList1.DataValueField = "Id";
DropDownList1.DataBind();

}

}

I hope it will help to someone.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.