在 Windows Phone 上解析在线 XML
我有这个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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你走在正确的轨道上。这给了我一个远离我的地方的寿司店的漂亮列表:(
如果有人知道处理这些命名空间的更好方法,请启发我。我正在使用
LocalName
来绕过它们。)You are on the right track. This is giving me a nice list of Sushi bars far far away from my place:
(And if anybody knows a better way of dealing with these namespaces please enlighten me. I am using
LocalName
to bypass them.)使用 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 toxml.Descendants("query")
might be all you need...