开发者

Parsing XML data with XDocument / XElement

开发者 https://www.devze.com 2023-03-01 16:53 出处:网络
While trying to parse through an RSS feed for a blog, I am running into a problem. While every element goes into my class fine, the one containing the actual content is always empty.

While trying to parse through an RSS feed for a blog, I am running into a problem. While every element goes into my class fine, the one containing the actual content is always empty.

<content:encoded>THIS IS FULL OF HTML </content:encoded>

Thats the XML line that it doesn't seem to parse. Its also the only one with a colon, and the only one that contains HTML data. The others look like this.

<title>
An amazing Title
</title>
<link>
More Junk
</link>
<comments>
Comments and things
</comments>

My Code is below, which gets every other element fine. Any ideas?

allPosts = (from x in feed.Descendants("item")
                         select new blogPost
                         {
                             Creator = (string)x.Element("creator"),
                             Title = (string)x.Element("title"),
                             Published = DateTime.Parse((string)x.Element("pubDate")),
                             Content = (string)x.Element("content"),
                             Description = (string)x.Elemen开发者_StackOverflow社区t("description"),
                             Link = (string)x.Element("link"),
                         }).ToList<blogPost>();

Thanks


Looks like you're looking for content and not for encoded. Content is the XML Namespace associated with the encoded element. What you need is to define a proper XNamespace for it and adding it to your query:

XNamespace contentNS = "<whatever the namespace is>";

allPosts = (from x in feed.Descendants("item")
                         select new blogPost
                         {
                             Creator = (string)x.Element("creator"),
                             Title = (string)x.Element("title"),
                             Published = DateTime.Parse((string)x.Element("pubDate")),

                             // Looking for content:encoded
                             Content = (string)x.Element(contentNS + "encoded"),

                             Description = (string)x.Element("description"),
                             Link = (string)x.Element("link"),
                         }).ToList<blogPost>();

The value of contentNS depends on what is present in your original XML, try to look for a xmlns:content definition in the root element.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号