ASP.NET DropDownList bind to XML and insert first item


XDocument doc = XDocument.Load(filePath);
var ds = (from model in doc.Descendants("model")     
		  select new     
			  {      
				  ID = model.Attribute("id").Value,      
				  Name = model.Attribute("name").Value     
			  }
		  ).OrderBy(m => m.Name).ToList();    

ddlExample.DataSource = ds; 
ddlExample.DataValueField = "ID"; 
ddlExample.DataTextField = "Name"; 
ddlExample.DataBind(); 
ddlExample.Items.Insert(0, new ListItem("Search by model", string.Empty));


Leave a Reply

Your email address will not be published. Required fields are marked *