2012-10-15 117 views
1

嘗試讀取ARSTECHNICA RSS供稿時,我不斷收到此錯誤。我收到以下錯誤:讀取RSS時出錯

System.Xml.XmlException: '', hexadecimal value 0x10, is an invalid character. Line 96, position 1129.

我知道這是因爲XML中的無效字符,它需要刪除。我確實在查看this以刪除無效的字符。

XmlReader readXML; 
    readXML = XmlReader.Create(URL); 
    SyndicationFeed News= SyndicationFeed.Load(readXML); 
    readXML.Close(); 
    return News; 

我不知道如何從RSS提要中刪除無效字符。有什麼建議麼?

感謝

+1

也許它是一個比實際無效字符更多的編碼問題?嘗試明確加載它作爲Utf8內容 –

回答

4

你可以嘗試加載RSS提要作爲一個字符串,刪除無效字符和負載作爲新聞源。希望有所幫助:

HttpWebRequest httpWebRequest = 
(HttpWebRequest)WebRequest.Create("http://feeds.arstechnica.com/arstechnica/index"); 
HttpWebResponse httpWebesponse = (HttpWebResponse)httpWebRequest.GetResponse(); 

Stream dataStream = httpWebesponse.GetResponseStream(); 
StreamReader streamreader = new StreamReader(dataStream, Encoding.UTF8); 
string response = streamreader.ReadToEnd(); 
streamreader.Close(); 

response = response.Replace((char)0x10, ' '); 

XmlTextReader reader = new XmlTextReader(new System.IO.StringReader(response)); 
reader.Read(); 

SyndicationFeed News = SyndicationFeed.Load(reader);