c# - Populating a dropdown with another -


i populating 1 ddl if select country name in ddl corresponding state must appear in ddl below code

protected void dropdownlist1_selectedindexchanged(object sender, eventargs e) {     string  st = (dropdownlist1.selectedindex).tostring();      xdocument main = xdocument.load((server.mappath(@"xmlfile1.xml")));      var query = user in main.descendants("country")                  st == user.element("state").value                  select user;      dropdownlist2.datasource = query;     dropdownlist2.databind();  } 

but unable populate states in ddl can me this?

i have changed query as:

var query = user in main.root.descendants("country")             user.elements("state").any() && user.elements("state").first().value == st              select user; 

please note, took descendants of root elements , check state element exist of country , check state value given value. if match found country element selected.

and return xmlelements, can't directly bind them datasource, query can select country name , , convert list or array. result of above can directly bind dropdownlist

here sample xml:

<?xml version="1.0" encoding="utf-8" ?> <countries>   <country>test1</country>   <country name ="a">     <state>test2</state>   </country>   <country name ="b">     <state>test2</state>   </country>   <country name ="c">     <state>test3</state>   </country> </countries> 

load xml

xdocument main = xdocument.load("input.xml"); 

get country names from country element attribute

var countrynames = (from user in main.root.descendants("country")                     user.elements("state").any() && user.elements("state").first().value == "test2"                     select user.attribute("name").value).tolist(); 

edit:

your xml:

<country>   <name>india</name>   <state>     <text>maharashtra</text>     <text>kashmir</text>     <text>goa</text>   </state> </country> 

updated code:

var countrynames = (from user in main.descendants("country")                     user.elements("state").descendants("text").any(s => s.value == st)                     select user.element("name").value).tolist(); 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -