2012-06-25 171 views
0

我收到以下錯誤:十六進制值0x1F的,是無效字符

「」,十六進制值0x1F的,是無效字符

這裏是我的功能。我第一次遇到「reader.MoveToContent()」時出現此錯誤。任何人都可以將我指向正確的方向嗎?

Public Function GetSyndicationFeedData(ByVal urlFeedLocation As String) As SyndicationFeed 

    Dim settings As New XmlReaderSettings() With { _ 
     .IgnoreWhitespace = True, _ 
     .CheckCharacters = True, _ 
     .CloseInput = True, _ 
     .IgnoreComments = True, _ 
     .IgnoreProcessingInstructions = True _ 
    } 

    If [String].IsNullOrEmpty(urlFeedLocation) Then 
     Return Nothing 
    End If 

    Using reader As XmlReader = XmlReader.Create(urlFeedLocation, settings) 
     If reader.ReadState = ReadState.Initial Then 
      reader.MoveToContent() 
     End If 

     ' Now try reading... 
     Dim atom As New Atom10FeedFormatter() 
     Dim rss20 As New Rss20FeedFormatter() 

     ' Atom 
     If atom.CanRead(reader) Then 
      atom.ReadFrom(reader) 
      Return atom.Feed 
      'Rss 2.0 
     ElseIf rss20.CanRead(reader) Then 
      rss20.ReadFrom(reader) 
      Return rss20.Feed 
     Else 
      Return Nothing 
     End If 
    End Using 
End Function 
+1

您是否檢查了Feed以確保它是有效的XML文檔? – Kane

+0

我沒有檢查http://validator.w3.org上的提要,它是有效的。據我瞭解,0x1f是一個Windows控制字符。有些人建議使用替換((char)(0x1F),''),但我不知道如何在這裏實現... –

回答

2

您可以使用下面的代碼(抱歉,但它是在C#中)來下載飼料並替換違規字符。

var client = new WebClient(); 
var feedAsString = client.DownloadString(urlFeedLocation).Replace((char)(0x1F), Convert.ToChar("")); 
using (XmlReader reader = XmlReader.Create(new MemoryStream(Encoding.Default.GetBytes(feedAsString)), settings)) 
{ 
    // remainder of your code here 
} 
相關問題