首先,您需要将不可靠的 XML 读入字节数组。然后将其转换为指定字符编码的字符串。
像这样
Using fsSource As FileStream = New FileStream(pathSource, _
FileMode.Open, FileAccess.Read)
' Read the source file into a byte array.
Dim bytes() As Byte = New Byte((fsSource.Length) - 1) {}
Dim numBytesToRead As Integer = CType(fsSource.Length,Integer)
Dim numBytesRead As Integer = 0
While (numBytesToRead > 0)
' Read may return anything from 0 to numBytesToRead.
Dim n As Integer = fsSource.Read(bytes, numBytesRead, _
numBytesToRead)
' Break when the end of the file is reached.
If (n = 0) Then
Exit While
End If
numBytesRead = (numBytesRead + n)
numBytesToRead = (numBytesToRead - n)
End While
numBytesToRead = bytes.Length
Dim strText As String = System.Text.Encoding.GetEncoding(1252).GetString(bytes)
End Using
我在这里使用的是 windows-1252,但您需要将其更改为这些文件的编码。