2010-07-01 23 views
1

我正在使用RSS源作爲數據源。我已經調整了如何檢索數據以使用WebClient.DownloadStringAsync方法。在我使用XmlReader.Create方法之前。然後,我使用結果作爲數據源來綁定到WPF中的TextBlock。爲什麼當XmlReader結果不會導致DownloadStringAsync結果具有奇怪的字符?

由於我在顯示結果時進行了更改,所有特殊編碼字符都顯示爲奇數字符。會喜歡一些幫助確保我保持正確的編碼,以便我的顯示值不會有奇怪的字符。

// WebClient code...  
private void GetFeed(int i) 
      { 
       Uri _feedUri = new Uri(_feedList[i]); 
       webClient = new WebClient(); 
       webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(stringStringCompletedEvent); 
       webClient.DownloadStringAsync(_feedUri, _feedTokenList[i]); 
      } 
private void stringStringCompletedEvent(object sender, DownloadStringCompletedEventArgs e) 
     { 
      if (e.Error == null) 
      { 

       string xmlString = e.Result; 
       XmlDocument doc = new XmlDocument(); 
       doc.LoadXml(xmlString); 
       doc.Save(@"C:\CashierData\msnbc-top.xml"); 
      } 
     } 

這是我以前使用XmlReader下載和解析提要的代碼。

XmlReader reader = XmlReader.Create(feed, settings); 

回答

2

我懷疑問題是Web服務器沒有正確指定內容編碼。

你可以使用DownloadDataAsync代替,然後使用

doc.Load(new MemoryStream(data)); 

(其中data是字節數組)。然後,XML解析器將自動檢測二進制數據中的XML編碼,而不是相信Web服務器知道正確的編碼。

+0

謝謝,我會試試 – discorax 2010-07-01 21:00:47

+0

這工作。謝謝。 – discorax 2010-07-01 22:12:09