2010-11-09 37 views
2

我消耗了RSS源,并在文档中包含特殊字符»加载XML文档失败,特殊字符»

我猜饲料不正确编码,但我不能改变的。我想重写一下,或者只是用友好的东西替换冒犯的字符。

using (Stream stream = response.GetResponseStream()) 
     { 

      using (XmlReader reader = XmlReader.Create(stream)) 
      { 
       try 
       { 
        XmlDocument xmlDoc = new XmlDocument(); 
        xmlDoc.Load(reader); //<--- FAILS HERE 
        //parse the items of the feed 

...

+0

你可以检查这个帖子 [http://stackoverflow.com/questions/700686/an-error-occurred-while-parsing-entityname](http://stackoverflow.com/questions/700686/an-error-occurrence-while-parsing-entityname) – 2011-01-11 16:11:06

回答

6

&raquo;HTML named entity和XML不支持。开箱即用,XML only supports&amp;,&apos;, &quot;,&gt;&lt;

改为使用相应的数字实体&#187;(或十六进制&#xbb;)。

1

+1Frédéric说的。您还可以将»作为原始未转义字符提供,大概以UTF-8编码。

如果是别人的RSS提要,您需要启动它们以停止生成格式不正确的XML;没有XML解析器会读这个。

<description>元素中,HTML内容通常应该是XML转义的。因此,如果项目的描述是This is a <em>really</em> interesting article,它应该出现在XML为:

<description>This is a &lt;em>really&lt;/em> interesting article</description> 

因此,HTML编码»字符应该站出来为

&amp;raquo; 

如果有人直接计入从一个HTML源代码不被转义,这是一个更严重的XML注入问题。

(这是假定的RSS 2.0在各种早期版本的RSS中,<description>是否包含HTML或纯文本从规范到规范各不相同,并且有时完全没有指定。对于旧的RSS版本,使用HTML内容并不可靠)

+0

我想我们应该踢谷歌为这些问题抽出RSS 2然后:http://feeds.feedburner.com/robinsloan – Nariman 2012-03-27 17:06:33