I'm trying to load country names and their cities from an XML file in silver-light.
I want the cities to be loaded to a ListPicker according to the selected country.
This is a part of my XML file:
<Times>
<country name="USA">
<city name="Aaronsburg -- PA">
<state>PA</state>
<latitude>408739</latitude>
<longitude>-773815</longitude>
<timezone>-500</timezone>
<daylight>1</daylight>
</city>
<city name="Abbeville -- AL">
<state>AL</state>
<latitude>316077</latitude>
<longitude>-853051</longitude>
<timezone>-600</timezone>
<daylight>1</daylight>
</city>
<city name="Abbeville -- GA">
<state>GA</state>
<latitude>319710</latitude>
<longitude>-833016</longitude>
<timezone>-500</timezone>
<daylight>1</daylight>
</city>
<city name="Abbot -- ME">
<state>ME</state>
<latitude>453219</latitude>
<longitude>-695342</longitude>
<timezone>-500</timezone>
<daylight>1</daylight>
</city>
........
........
and this the code I wrote:
private void LoadButton_Click(object sender, RoutedEventArgs e)
{
XDocument doc = XDocument.Load("Athan.xml");
var definitions = doc.Document.Descendants(XName.Get("country"));
foreach (var definition in definitions)
{
if (definition.Attribute(XName.Get("name")).Value == CountryListPicker.SelectedItem.ToString())
{
var cities = definition.Document.Descendants(XName.Get("city"));
foreach (var city in cities)
{
CityListPicker.Items.Add(city.Attribute(XName.Get("name")).Value.ToString());
}
return;
}
}
}
cities are loaded after a long time, or not loaded! is something wrong with my code?
OK, I might be completely off-base here, because I don't know anything about windows-phone-7, but I'm assuming that the core of your LoadButton_Click method is Linq to Xml.
If that's the case, I wonder among other things about your calls to .Document, and the frequent calls to XName. For example, it seems to me that your code for definitions could probably be something simple like "var definitions = doc.Descendants("country");" Similarly for the "var cities" line.
When dealing with attributes, you might be able to get away with something like "CityListPicker.Items.Add(city.Attribute("name")).Value;"
Because data is loading at least some of the time, I assume you don't have namespace issues, but you should verify.
Hope this helps.
Try to add
XDocument doc = XDocument.Load("Athan.xml");
XElement root = doc.Root;
foreach (XElement el in root.Descendants("Times"))
{
// code to navigate in your XML
if ( el.Name == "country"){
foreach ( XAttribute attr in el.Attributes())
{
if ( attr.name == "name"){
// receive your country name
String CountryName = attr.Value;
}
}
foreach (XElement city in el.Descendants())
{
if (city.Name == "city")
{
Object City = new Object(); // create your object...
foreach ( XAttribute attr in el.Attributes())
{
if ( attr.name == "state"){
City.sate = cityAttr.Value;
}
if ( attr.name == "latitude"){
City.latitude = cityAttr.Value;
}
// etc....
}
//You add your object city in list ( for example ) ...
}
}
I have problems to load my xml , and with this method, xml was loaded...
Hope this solution help you...