简体   繁体   中英

Load XML into dropdownlist C#

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);
}  

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,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM