在 Windows Phone 上解析在线 XML

发布于 2024-12-16 18:34:38 字数 1523 浏览 0 评论 0原文

我有这个XML 我想解析它以在我的 WP 应用程序中使用。

这就是我所做的:

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

问题出在这一行:

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

虽然我没有丢失任何引用...

这是解析 XML 的好方法吗?

我应该使用 LINQ to XML 还是 XmlReader

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

2024-12-23 18:34:38

你走在正确的轨道上。这给了我一个远离我的地方的寿司店的漂亮列表:(

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

如果有人知道处理这些命名空间的更好方法,请启发我。我正在使用 LocalName 来绕过它们。)

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

赢得她心 2024-12-23 18:34:38

使用 LINQ to XML 没问题...但是您链接到的 URL 仅具有单个查询元素,并且是 ,而不是 。目前还不清楚您出了什么问题,但更改为 xml.Descendants("query") 可能就是您所需要的......

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文