Diego Vin
Diego Vin

Reputation: 293

Parsing online XML on Windows Phone

I have this XML and I want to parse it to use in my WP app.

This is what I did:

    private void button1_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        WebClient client = new WebClient();
        client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
        Uri url = new Uri("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20local.search%20where%20query%3D%22sushi%22%0A%20%20%20and%20location%3D%22san%20francisco%2C%20ca%22%0A%20%20%20and%20Rating.AverageRating%3E4.0%0A&diagnostics=true", UriKind.Absolute);
        client.OpenReadAsync(url);
    }


    public void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
    {
        try
        {
            var xml = XDocument.Load(e.Result);
            var query = from c in xml.Descendants("Query")
                        select new
                        {
                           ...
                        };
        }
        catch (Exception c)
        {
            MessageBox.Show(c.Message);
        }
    }

The problem is in this line:

var query = from c in xml.Descendants("Query")

Although I'm not missing any references...

Is this a good way to parse a XML?

Should I use LINQ to XML or XmlReader?

Upvotes: 0

Views: 2217

Answers (2)

Heinrich Ulbricht
Heinrich Ulbricht

Reputation: 10362

You are on the right track. This is giving me a nice list of Sushi bars far far away from my place:

public void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
    try
    {
        var xml = XDocument.Load(e.Result);
        // get all the result rows from the query (they come as <Result> elements as child elements of <Results> which in turn is a child of <query>)
        var results = from row in xml.Element("query").Element("results").Elements().Where( element => { return (element.Name.LocalName == "Result"); } )
                      select row;

        // now I loop all rows and print the title; of course you can
        // do other stuff here or combine some data processing with the LINQ above
        // - this is up to you
        foreach (var result in results)
        {
            XElement title = result.Elements().Where(element => { return element.Name.LocalName == "Title"; }).FirstOrDefault();
            if (title != null)
                Debug.WriteLine(title.Value);
        }
    }
    catch (Exception c)
    {
        MessageBox.Show(c.Message);
    }
}

(And if anybody knows a better way of dealing with these namespaces please enlighten me. I am using LocalName to bypass them.)

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1502036

Using LINQ to XML is fine... but the URL you linked to only has a single query element, and it's <query>, not <Query>. It's not really clear what's going wrong for you, but changing to xml.Descendants("query") might be all you need...

Upvotes: 2

Related Questions