Reading wordpress RSS with C# - Content different

不羁的心 提交于 2019-12-12 03:09:17

问题


I'm trying to read a RSS generated by wordpress with full text activated. On firefox and IE9 an item data contains the element content:encoded:

<content:encoded><![CDATA[bla bla bla]]></content:encoded>            

but when in a C# program I request the same rss url this node is not present. I do my C# request like this:

   WebClient client = new WebClient();
   client.Encoding = Encoding.UTF8;
   client.Headers.Add("Accept", "application/xml");
   var xml = client.DownloadString(url)

Does I have to add an header to the request to have this specific field?


回答1:


You don't need WebClient to download rss.

XDocument wp = XDocument.Load("http://wordpress.org/news/feed/");
XNamespace ns = XNamespace.Get("http://purl.org/rss/1.0/modules/content/");

foreach (var content in wp.Descendants(ns + "encoded"))
{
    Console.WriteLine(System.Net.WebUtility.HtmlDecode(content.Value)+"\n\n");
}

EDIT

The problem is related with compression. If the client doesn't support compression, then server doesn't send contents.

WebClient web = new WebClient();
web.Headers["Accept-Encoding"] = "gzip,deflate,sdch";

var zip = new System.IO.Compression.GZipStream(
    web.OpenRead("http://www.whiskymag.fr/feed/?post_type=sortir"), 
    System.IO.Compression.CompressionMode.Decompress);

string rss = new StreamReader(zip, Encoding.UTF8).ReadToEnd();



回答2:


I'm guessing Wordpress is choosing the "wrong" output format based on your Accept header. Which feed is used is decided in /wp-content/feed.php:

$types = array(
    'rss'  => 'application/rss+xml',
    'rss2' => 'application/rss+xml',
    'rss-http'  => 'text/xml',
    'atom' => 'application/atom+xml',
    'rdf'  => 'application/rdf+xml'
);

so instead of text/xml, try accepting application/rss+xml.



来源:https://stackoverflow.com/questions/9329067/reading-wordpress-rss-with-c-sharp-content-different

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!