使用 Digg API 将 XML 转为 LINQ
继续我之前的问题(http://stackoverflow.com/questions/7817619/unknown-error-using-digg-api-and-uri-handler-silverlight,我想感谢您的回答)我现在有一个关注少提出错误的问题。
为了从 this 中提取数据,我们得到了以下 XML到 LINQ 代码。
var stories = from story in document.Descendants("story")
//where story.Element("thumbnail") != null
select new DiggStory
{
Id = (string)story.Attribute("story_id"),
Title = (string)story.Element("title"),
Description = (string)story.Element("description"),
ThumbNail = (string)story.Element("thumbnail").Attribute("src"),
//HrefLink = (string)story.Attribute("permalink"),
NumDiggs = (int)story.Attribute("diggs")
};
这按预期工作,但鉴于 Digg API 已过时,我更愿意成为新的 api,它创建 以下 XML 文件。
现在我想知道如何调整 XML 到 LINQ 代码以利用这个新的 XML 文件? 我知道问题出在
var stories = from story in document.Descendants("story")
但我不知道需要将其更改为什么,因为 新的 XML 文件 有更多级别。我在想“
var stories = from item in document.Descendants("stories")
但这似乎行不通”。
我想再次感谢您帮助我解决这个问题以及任何其他问题,这确实是一个很棒的网站!
谢谢你,托马斯
Continuining from my previous question (http://stackoverflow.com/questions/7817619/unknown-error-using-digg-api-and-uri-handler-silverlight, which I want to thank you for answering) I now have a follow up less errory question.
To extract the data from this we got the following XML to LINQ code.
var stories = from story in document.Descendants("story")
//where story.Element("thumbnail") != null
select new DiggStory
{
Id = (string)story.Attribute("story_id"),
Title = (string)story.Element("title"),
Description = (string)story.Element("description"),
ThumbNail = (string)story.Element("thumbnail").Attribute("src"),
//HrefLink = (string)story.Attribute("permalink"),
NumDiggs = (int)story.Attribute("diggs")
};
This works as intended, but seeing as the Digg API is outdated I would prefer to be the new api, which creates the following XML file.
Now I was wondering how do I adjust the XML to LINQ code to utilize this new XML file?
I know the problem is in
var stories = from story in document.Descendants("story")
But I do not know what I need to change it to, because the new XML file has more levels. I was thinking something in the line of
var stories = from item in document.Descendants("stories")
But that doesn't seem work.
I want to thank you again for helping me with this problem and any further problems, this is truly a great website!
Thank you, Thomas
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从这里开始:
您可以了解有关使用 LINQ to XML。对您特别有用的可能是 XElement 文档,它显示了为 XML 文档中的每个“元素”(标记)定义的所有方法和属性。
Start with this:
You can learn more about parsing XML documents with LINQ to XML. Of particular use to you is probably the XElement documentation, which shows all the methods and properties defined for each "element" (tag) in the XML document.