LINQ查询问题

发布于 2024-11-09 02:55:54 字数 399 浏览 0 评论 0 原文

无法在 Feed 中获得任何结果。 feedXML 具有正确的数据。

XDocument feedXML = XDocument.Load(@"http://search.twitter.com/search.atom?q=twitter");

var feeds = from entry in feedXML.Descendants("entry")
            select new
            {
                PublicationDate = entry.Element("published").Value,
                Title = entry.Element("title").Value
            };

我缺少什么?

Can't get any result in feeds.
feedXML has the correct data.

XDocument feedXML = XDocument.Load(@"http://search.twitter.com/search.atom?q=twitter");

var feeds = from entry in feedXML.Descendants("entry")
            select new
            {
                PublicationDate = entry.Element("published").Value,
                Title = entry.Element("title").Value
            };

What am I missing?

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

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

发布评论

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

评论(4

巴黎盛开的樱花 2024-11-16 02:55:54

您需要指定命名空间:

// This is the default namespace within the feed, as specified
// xmlns="..." 
XNamespace ns = "http://www.w3.org/2005/Atom";

var feeds = from entry in feedXML.Descendants(ns + "entry")
            ...

与我使用过的一切其他 XML API 相比,LINQ to XML 中的命名空间处理非常简单:)

You need to specify the namespace:

// This is the default namespace within the feed, as specified
// xmlns="..." 
XNamespace ns = "http://www.w3.org/2005/Atom";

var feeds = from entry in feedXML.Descendants(ns + "entry")
            ...

Namespace handling is beautifully easy in LINQ to XML compared with everything other XML API I've ever used :)

披肩女神 2024-11-16 02:55:54

您需要在 Descendents 和 Element 方法上指定命名空间。

XDocument feedXML = XDocument.Load(@"http://search.twitter.com/search.atom?q=twitter");

XNamespace ns = "http://www.w3.org/2005/Atom";
var feeds = from entry in feedXML.Descendants(ns + "entry")
            select new
            {
            PublicationDate = entry.Element(ns + "published").Value,
            Title = entry.Element(ns + "title").Value
            };

You need to specify a namespace on both the Descendents and Element methods.

XDocument feedXML = XDocument.Load(@"http://search.twitter.com/search.atom?q=twitter");

XNamespace ns = "http://www.w3.org/2005/Atom";
var feeds = from entry in feedXML.Descendants(ns + "entry")
            select new
            {
            PublicationDate = entry.Element(ns + "published").Value,
            Title = entry.Element(ns + "title").Value
            };
豆芽 2024-11-16 02:55:54

如果你查看 HTTP 请求返回的 XML,你会发现它定义了一个 XML 命名空间:

<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" ...>
  <id>tag:search.twitter.com,2005:search/twitter</id>
  ...
</feed>

XML 就像 C# 一样,如果你使用了错误命名空间的元素名称,它不会被认为是同一个元素!您需要将所需的命名空间添加到查询中:

private static string AtomNamespace = "http://www.w3.org/2005/Atom";

public static XName Entry = XName.Get("entry", AtomNamespace);

public static XName Published = XName.Get("published", AtomNamespace);

public static XName Title = XName.Get("title", AtomNamespace);

var items = doc.Descendants(AtomConst.Entry)
                .Select(entryElement => new FeedItemViewModel()
                new {
                  Title = entryElement.Descendants(AtomConst.Title).Single().Value,
                  ...
                });

If you look at the XML returned by the HTTP request, you will see that it has an XML namespace defined:

<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" ...>
  <id>tag:search.twitter.com,2005:search/twitter</id>
  ...
</feed>

XML is just like C#, if you use an element name with the wrong namespace, it is not considered to be the same element! You need to add the required namepsace to your query:

private static string AtomNamespace = "http://www.w3.org/2005/Atom";

public static XName Entry = XName.Get("entry", AtomNamespace);

public static XName Published = XName.Get("published", AtomNamespace);

public static XName Title = XName.Get("title", AtomNamespace);

var items = doc.Descendants(AtomConst.Entry)
                .Select(entryElement => new FeedItemViewModel()
                new {
                  Title = entryElement.Descendants(AtomConst.Title).Single().Value,
                  ...
                });
长途伴 2024-11-16 02:55:54

问题出在 中。这返回 0 个结果
根据文档,您需要输入完全限定的 XName

The issue is in feedXML.Descendants("entry"). This is returning 0 results
According to the documentation you need to put in a fully qualified XName

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