简体   繁体   中英

Reading wordpress RSS with C# - Content different

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?

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM