使用 Digg API 将 XML 转为 LINQ

发布于 2024-12-11 03:05:02 字数 1584 浏览 0 评论 0原文

继续我之前的问题(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 技术交流群。

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

发布评论

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

评论(1

痴梦一场 2024-12-18 03:05:02

从这里开始:

var stories = from item in document.RootElement.Element("stories").Descendants("item")
              select new DiggStory
              {
                  Id = item.Element("story_id").Value
                  // ...etc
              }

您可以了解有关使用 LINQ to XML。对您特别有用的可能是 XElement 文档,它显示了为 XML 文档中的每个“元素”(标记)定义的所有方法和属性。

Start with this:

var stories = from item in document.RootElement.Element("stories").Descendants("item")
              select new DiggStory
              {
                  Id = item.Element("story_id").Value
                  // ...etc
              }

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.

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