C# 在Windows Phone上解析联机XML
我有这个,我想解析它在我的WP应用程序中使用 这就是我所做的:C# 在Windows Phone上解析联机XML,c#,.net,silverlight,windows-phone-7,xml-parsing,C#,.net,Silverlight,Windows Phone 7,Xml Parsing,我有这个,我想解析它在我的WP应用程序中使用 这就是我所做的: private void button1_Click(object sender, System.Windows.RoutedEventArgs e) { WebClient client = new WebClient(); client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCom
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的好方法吗
我应该使用LINQtoXML还是
XmlReader
?使用LINQtoXML是可以的。。。但是您链接到的URL只有一个查询元素,它是
,而不是
。现在还不清楚您到底出了什么问题,但是更改为xml。子体(“查询”)
可能就是您所需要的一切…使用LINQ to xml很好。。。但是您链接到的URL只有一个查询元素,它是
,而不是
。现在还不清楚您到底出了什么问题,但更改为xml。您所需要的可能就是子体(“查询”)
。您走上了正确的道路。这给了我一张离我家很远的寿司店的清单:
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);
}
}
public void客户端\u OpenReadCompleted(对象发送方,OpenReadCompletedEventArgs e)
{
尝试
{
var xml=XDocument.Load(即结果);
//从查询中获取所有结果行(它们作为元素作为子元素出现,而子元素又是的子元素)
var results=来自xml.Element(“query”).Element(“results”).Elements()中的行,其中(Element=>{return(Element.Name.LocalName==“Result”);})
选择行;
//现在我循环所有行并打印标题;当然可以
//在这里做其他事情,或者将一些数据处理与上面的LINQ结合起来
//-这取决于你
foreach(结果中的var结果)
{
XElement title=result.Elements().Where(element=>{return element.Name.LocalName==“title”;}).FirstOrDefault();
如果(标题!=null)
Debug.WriteLine(title.Value);
}
}
捕获(例外c)
{
MessageBox.Show(c.Message);
}
}
(如果有人知道更好的处理这些名称空间的方法,请告诉我。我正在使用
LocalName
绕过它们。)您的思路是正确的。这给了我一张离我家很远的寿司店的清单:
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);
}
}
public void客户端\u OpenReadCompleted(对象发送方,OpenReadCompletedEventArgs e)
{
尝试
{
var xml=XDocument.Load(即结果);
//从查询中获取所有结果行(它们作为元素作为子元素出现,而子元素又是的子元素)
var results=来自xml.Element(“query”).Element(“results”).Elements()中的行,其中(Element=>{return(Element.Name.LocalName==“Result”);})
选择行;
//现在我循环所有行并打印标题;当然可以
//在这里做其他事情,或者将一些数据处理与上面的LINQ结合起来
//-这取决于你
foreach(结果中的var结果)
{
XElement title=result.Elements().Where(element=>{return element.Name.LocalName==“title”;}).FirstOrDefault();
如果(标题!=null)
Debug.WriteLine(title.Value);
}
}
捕获(例外c)
{
MessageBox.Show(c.Message);
}
}
(如果有人知道如何更好地处理这些名称空间,请告诉我。我正在使用
LocalName
绕过它们。)你说有问题,但你没有说问题出在哪里。发生了什么?你说有问题,但你没有说问题出在哪里。发生了什么?方法呢?我应该使用openReadAsync还是DownloadStringAsync?指向xml的链接抱怨说它在my.xap中找不到xml…@DiegoVin:为什么不首先使用XDocument.Load(url)
?如果e.Result
是一条流,那么我希望它不会有问题。。。您能给出准确的错误消息吗?XDocument.Load不会从Windows Phone上的web URL加载。就个人而言-我使用了DownloadStringAsync,因为它使调试更容易,但OpenReadAsync可能也能工作。@xyzer:有趣-我可以发誓我使用了WP7中的XDocument.Load(url)
。也许不是…@DiegoVin:不,这是一个普通的HTTP URL。不过我可能记错了。方法呢?我应该使用openReadAsync还是DownloadStringAsync?指向xml的链接抱怨说它在my.xap中找不到xml…@DiegoVin:为什么不首先使用XDocument.Load(url)
?如果e.Result
是一条流,那么我希望它不会有问题。。。您能给出准确的错误消息吗?XDocument.Load不会从Windows Phone上的web URL加载。就个人而言-我使用了DownloadStringAsync,因为它使调试更容易,但OpenReadAsync可能也能工作。@xyzer:有趣-我可以发誓我使用了WP7中的XDocument.Load(url)
。也许不是…@DiegoVin:不,这是一个普通的HTTP URL。但我可能记错了。我试着用你的例子,但当我尝试放置元素()时出错。其中。。。我在这里看不到where,即使我尝试在xml.Element(“query”).Element(“results”)中只放置var result=from行,也可以选择row;它返回一个错误:找不到源类型“System.Xml.Linq.Xelement”的查询模式的实现。选择“未找到”我已解决问题!我已经解决了这个问题!缺少对system.linq的引用。。。linqtoxml代码也是如此:XElement xml=XElement.Load(e.Result);var searched=来自xml中的c;foreach(搜索中的var项){listBox1.Items.Add(item.Value);}是的,您需要这个引用。很好,你找到了答案:)我试着用你的例子,但我在尝试放置元素()时出错。其中。。。我在这里看不到where,即使我尝试在xml.Element(“query”).Element(“results”)中只放置var result=from行