使用 linq 的原子命名空间

发布于 2024-08-09 12:00:05 字数 2153 浏览 9 评论 0原文

我有以下 xml

    <?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/">
<entry><id><![CDATA[text]]></id><
author><name><![CDATA[film24]]></name></author><title><![CDATA[text]]></title>
<updated>2009-10-30T15:55:13+00:00</updated><published>2009-10-30T00:00:00+00:00</published>
<media:thumbnail type="image/jpeg" title="thumbnail" url=""/>
<media:content type="video/flv" title="video" url="" expression="high"/>
<media:content type="video/flv" title="video" url="" expression="low"/>
</entry>
</feed>

如果我包含以下命名空间,我无法查询该 xml。即它不返回任何结果。

http://www.w3.org/2005/Atom

这是我查询xml的方式

        XNamespace nsMedia = "http://search.yahoo.com/mrss/";
        XNamespace nsAtom = "http://www.w3.org/2005/Atom";

        string url = HttpContext.Current.Server.MapPath(ConfigHelper.GetValue("FeedUri"));
        var feed = XElement.Load(url);
        var posts = from c in feed.Descendants(nsAtom + "entry")
                    select new CDNEntry
                    {
                         Id = (string)c.Element(nsAtom + "id").Value,
                         Author = (string)c.Element(nsAtom + "author").Value,
                         Title = (c.Element("title") != null) ? (string)c.Element(nsAtom + "title").Value : "",
                         Summary = (c.Element("summary") != null) ? (string)c.Element(nsAtom + "summary").Value : "",
                         Thumbnail = (string)c.Element(nsMedia + "thumbnail").Attribute(nsAtom + "url").Value,
                         FLV = (string)c.Element(nsMedia + "content").Attribute(nsAtom + "url").Value,
                         Updated = DateTime.Parse((string)c.Element(nsAtom + "updated").Value),
                         Published = DateTime.Parse((string)c.Element(nsAtom + "published").Value)
                    };

        return posts.ToList();

I have the following xml

    <?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/">
<entry><id><![CDATA[text]]></id><
author><name><![CDATA[film24]]></name></author><title><![CDATA[text]]></title>
<updated>2009-10-30T15:55:13+00:00</updated><published>2009-10-30T00:00:00+00:00</published>
<media:thumbnail type="image/jpeg" title="thumbnail" url=""/>
<media:content type="video/flv" title="video" url="" expression="high"/>
<media:content type="video/flv" title="video" url="" expression="low"/>
</entry>
</feed>

If i include the following namespace i cannot query the xml. ie it doesn't return any results.

http://www.w3.org/2005/Atom

Here is how i query the xml

        XNamespace nsMedia = "http://search.yahoo.com/mrss/";
        XNamespace nsAtom = "http://www.w3.org/2005/Atom";

        string url = HttpContext.Current.Server.MapPath(ConfigHelper.GetValue("FeedUri"));
        var feed = XElement.Load(url);
        var posts = from c in feed.Descendants(nsAtom + "entry")
                    select new CDNEntry
                    {
                         Id = (string)c.Element(nsAtom + "id").Value,
                         Author = (string)c.Element(nsAtom + "author").Value,
                         Title = (c.Element("title") != null) ? (string)c.Element(nsAtom + "title").Value : "",
                         Summary = (c.Element("summary") != null) ? (string)c.Element(nsAtom + "summary").Value : "",
                         Thumbnail = (string)c.Element(nsMedia + "thumbnail").Attribute(nsAtom + "url").Value,
                         FLV = (string)c.Element(nsMedia + "content").Attribute(nsAtom + "url").Value,
                         Updated = DateTime.Parse((string)c.Element(nsAtom + "updated").Value),
                         Published = DateTime.Parse((string)c.Element(nsAtom + "published").Value)
                    };

        return posts.ToList();

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

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

发布评论

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

评论(1

与往事干杯 2024-08-16 12:00:05

您的示例 XML 被截断,但您的问题很可能是您需要使用更多命名空间。尝试这样的事情:

XNamespace nsMedia = "http://search.yahoo.com/mrss/";
XNamespace nsAtom = "http://www.w3.org/2005/Atom";

var feed = GetFeed();
var posts = from c in feed.Descendants(nsAtom + "entry")
            where (string)c.Element(nsAtom + "id") == uniqueUrl
            select new CDNEntry
            {
                Id = (string)c.Element(nsAtom + "id"),
                Author = (string)c.Element(nsAtom + "author"),
                Title = (string)c.Element(nsAtom + "title") ?? "",
                Summary = (string)c.Element(nsAtom + "summary") ?? "",
                Thumbnail = (string)c.Element(nsMedia + "thumbnail").Attribute("url"),
                FLV = (string)c.Element(nsMedia + "content").Attribute("url"),
                Updated = (DateTime)c.Element(nsAtom + "updated"),
                Published = (DateTime)c.Element(nsAtom + "published")
            };

Your sample XML got cut off, but your problem is most likely that you need to use more namespaces. Try something like this:

XNamespace nsMedia = "http://search.yahoo.com/mrss/";
XNamespace nsAtom = "http://www.w3.org/2005/Atom";

var feed = GetFeed();
var posts = from c in feed.Descendants(nsAtom + "entry")
            where (string)c.Element(nsAtom + "id") == uniqueUrl
            select new CDNEntry
            {
                Id = (string)c.Element(nsAtom + "id"),
                Author = (string)c.Element(nsAtom + "author"),
                Title = (string)c.Element(nsAtom + "title") ?? "",
                Summary = (string)c.Element(nsAtom + "summary") ?? "",
                Thumbnail = (string)c.Element(nsMedia + "thumbnail").Attribute("url"),
                FLV = (string)c.Element(nsMedia + "content").Attribute("url"),
                Updated = (DateTime)c.Element(nsAtom + "updated"),
                Published = (DateTime)c.Element(nsAtom + "published")
            };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文