How can I read this XML file in WP7?

Go To StackoverFlow.com

0

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?

2012-04-04 02:15
by Hamzeh Soboh


0

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.

2012-04-04 03:07
by Steve
Thanks for help, I'm using system.xml.linq, I think that the code brings all data into memory, what makes the application stuck! I think there is some way to read an xml file without loading it entirely into memory. Thank - Hamzeh Soboh 2012-04-04 03:14
If the issue is with loading the file into memory, perhaps this link will help: http://stackoverflow.com/questions/5838657/how-can-i-use-linq-to-xml-to-query-huge-xml-files-with-reasonable-memory-consum - Steve 2012-04-04 03:35


0

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...

2012-04-05 09:59
by Doc Roms
Ads