how to populate city corresponding selected state
i have a xml below.now i want to populate all city in drop downlist according to selected state(code is given below)but i am getting only first one city i开发者_开发知识库nstead of all city. how to get all city.please see code below.
<?xml version="1.0" encoding="utf-8" ?>
<states>
<state>
<stateid>1</stateid>
<city>umr</city>
<city>kat</city>
<city>jpl</city>
<city>bpl</city>
</state>
<state>
<stateid>2</stateid>
<city>mumbai</city>
<city>dadar</city>
<city>ghat</city>
<city>kanjur</city>
</state>
</states>
here 1 is stateid,with in state have city like umr.kat,jpl bpl.
public static List<statecs> GetStateFromXML(string getstateid)
{
XDocument xmlDoc = XDocument.Load(
HttpContext.Current.Server.MapPath("stateXML.xml"));
var states = from state in xmlDoc.Descendants("state")
where state.Element("stateid").Value == getstateid
select new statecs { City = state.Element("city").Value, };
return states.ToList();
}
this might work...not tested nor compiled
// fetch states first
var states = from state in xmlDoc.Descendants("state")
where state.Element("stateid").Value == getstateid
select state;
// for found states select the city.
var cities = from city in states.Descendants("city")
select new statecs { City = city.Value, };
return cities.ToList();
You can use SelectMany:
return xmlDoc.Descendants("state")
.Where(state => state.Element("stateid").Value == getstateid)
.SelectMany(state => state.Elements("city").Select(city => new statecs { City = city.Value }))
.ToList();
精彩评论