Reputation: 813
I want to add all the city names into a dropdownlist. In my code I only get the first name loaded. How do I change the code so that I get all?
My XML:
<country>
<city>
<cityname>Cannes</cityname>
<cityid>123</cityid>
</city>
<city>
<cityname>Paris</cityname>
<cityid>123</cityid>
</city>
<city>
<cityname>Nice</cityname>
<cityid>123</cityid>
</city>
<city>
<cityname>Marseilles</cityname>
<cityid>123</cityid>
</city>
</country>
My code:
XElement country= XElement.Load(Server.MapPath("myXML.xml"));
foreach (XElement name in country.Element("city").Elements("cityname"))
{
dropdownList.Items.Add(name.Value);
}
Upvotes: 1
Views: 2620
Reputation: 5820
You forgot a simple 'S' in your code.
I've just tested it and this works:
XElement country= XElement.Load(Server.MapPath("myXML.xml"));
foreach (XElement name in country.Elements("city").Elements("cityname"))
{
dropdownList.Items.Add(name.Value);
}
Kind regards,
Upvotes: 2