![](/img/trans.png)
[英]Error reading reading RSS (0.91) content using C# 4.0 SyndicationFeed
[英]Reading wordpress RSS with C# - Content different
我正在嘗試閱讀由wordpress生成的RSS,其中包含全文本。 在Firefox和IE9上,項目數據包含元素content:encoded
:
<content:encoded><![CDATA[bla bla bla]]></content:encoded>
但是在C#程序中我請求相同的rss url時,此節點不存在。 我這樣執行我的C#請求:
WebClient client = new WebClient();
client.Encoding = Encoding.UTF8;
client.Headers.Add("Accept", "application/xml");
var xml = client.DownloadString(url)
我是否必須在請求中添加標頭才能具有此特定字段?
您不需要WebClient來下載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");
}
編輯
問題與壓縮有關。 如果客戶端不支持壓縮,則服務器不會發送內容。
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();
我猜Wordpress正在根據您的Accept
標頭選擇“錯誤的”輸出格式。 使用哪個供稿由/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'
);
因此,請嘗試接受application/rss+xml
而不是text/xml
application/rss+xml
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.