Here is xml data:-
xsi:noNamespaceSchemaLocation="employee.xs">
and c# code to bind data to dropdowlist:-
protected void Page_Load(object sender, EventArgs e)
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("~/XMLDataFile.xml"));
ddlEmp_List.DataSource=ds;
ddlEmp_List.DataTextField = "Name";
ddlEmp_List.DataValueField = "Employee_Number";
ddlEmp_List.DataBind ();
}
from there xml data I want to show only if "Active=True" employee name. How can do?


Manas MohapatraRank #182 · 13.2K reputationPosted Jan 28, 2016, 12:09 AM EST
Shubham KumarRank #621 · 3.8K reputationPosted Jan 27, 2016, 10:55 PM EST
var doc = XDocument.Load("XMLFile1.xml"); var res = new XDocument (new XElement("data", (from i in doc.Root.Elements() where i.Element(XName.Get("Active")).Value == "True" select i))); var xml = res.ToString(); yourdatatable = (DataTable)xml yourdropdown.datasource=dt; youdropdown.databind();and convert var in datatable and pass it in datasource
Abhilash J ARank #1,243 · 1.4K reputationPosted Jan 28, 2016, 12:01 AM EST
I am modified code like this:-
protected void Page_Load(object sender, EventArgs e)
{
var doc = XDocument.Load(Server.MapPath("~/XMLDataFile.xml"));
var res = new XDocument
(new XElement("Name", "Employee_Number",
(from i in doc.Root.Elements()
where i.Element(XName.Get("Active")).Value == "True"
select i)));
var xml = res.ToString();
DataTable dt = LINQToDataTable(xml);
ddlEmp_List.DataSource = dt;
ddlEmp_List.DataBind();
}
public DataTable LINQToDataTable
{
DataTable dtReturn = new DataTable();
// column names
PropertyInfo[] oProps = null;
if (varlist == null) return dtReturn;
foreach (T rec in varlist)
{
// Use reflection to get property names, to create table, Only first time, others will follow
if (oProps == null)
{
oProps = ((Type)rec.GetType()).GetProperties();
foreach (PropertyInfo pi in oProps)
{
Type colType = pi.PropertyType;
if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition()
== typeof(Nullable<>)))
{
colType = colType.GetGenericArguments()[0];
}
dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
}
}
DataRow dr = dtReturn.NewRow();
foreach (PropertyInfo pi in oProps)
{
dr[pi.Name] = pi.GetValue(rec, null) == null ? DBNull.Value : pi.GetValue
(rec, null);
}
dtReturn.Rows.Add(dr);
}
return dtReturn;
}
but it return System.Data.DataRowView like that. How to show only Active=true employee name from xml file?