我正在尝试将一段代码从 C# 转换为 VB.NET。原始代码示例可以位于此处(请参阅接受的答案):
在构造使用数据的 XmlReader 或 XPathDocument 之前,如何从基于 XML 的数据源中删除无效的十六进制字符?
我已经使用在线工具ch
进行了粗略的转换,尽管在针对十六进制字符范围检查 char 对象时出现异常。
If (ch < &HFD AndAlso ch > &H1F)
引发的异常是operator < is not defined for types 'char' and 'integer'
. VB 解释哪里出错了?
Function RemoveTroublesomeCharacters(inString As String) As String
If inString Is Nothing Then
Return Nothing
End If
Dim newString As New StringBuilder()
Dim ch As Char
For i As Integer = 0 To inString.Length - 1
ch = inString(i)
' remove any characters outside the valid UTF-8 range as well as all control characters
' except tabs and new lines
If (ch < &HFD AndAlso ch > &H1F) OrElse ch = ControlChars.Tab OrElse ch = ControlChars.Lf OrElse ch = ControlChars.Cr Then
newString.Append(ch)
End If
Next
Return newString.ToString()
End Function