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();
4

1 に答える 1

2

サンプル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")
            };
于 2009-10-31T03:00:37.717 に答える