問題
An Invalid character was found in text content.
解決方法
The following are the character ranges for low-order non-printable ASCII characters that are rejected by MSXML versions 3.0 and later:
#x0 - #x8 (ASCII 0 - 8)
#xB - #xC (ASCII 11 - 12)
#xE - #x1F (ASCII 14 - 31)
以上定義說明了在XML理不支援的ASCII,因此解決方式有2種:
先將不支援的ASCII列出
Private Function AsciiLowOrderList() As List(Of Char) Dim filter As List(Of Char) = New List(Of Char) For i As Integer = 0 To 31 If i <> 9 Or i <> 10 Or i <> 13 Then filter.Add(Chr(i)) End If Next Return filter End Function1.轉成CHAR,再逐一比對
Private Function AsciiLowOrderFilterByChr(ByVal strVal As String) As String Dim chrs As Char() = strVal.ToCharArray() Dim buffer As StringBuilder = New StringBuilder(strVal.Length) Dim filter As List(Of Char) = AsciiLowOrderList() For Each chr As Char In chrs If filter.Contains(chr) = False Then buffer.Append(chr) End If Next Return buffer.ToString() End Function2.採用REPLACE方式避開此問題
Private Function AsciiLowOrderFilterByReplace(ByVal strVal As String) As String For Each chr As Char In AsciiLowOrderList() strVal = strVal.Replace(chr,string.Empty) Next Return strVal End Function
沒有留言:
張貼留言