有人知道为什么流阅读器不能读取上标字符(VB.NET) [英] Anybody know why stream reader not reading superscript characters(VB.NET)

查看:65
本文介绍了有人知道为什么流阅读器不能读取上标字符(VB.NET)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用StreamReader将Tab Delim Text文件中的数据导入名为Import Data的数据表。由于某些原因,一旦导入到表中,所有superScript字符都不可读。



例如如果我有一个ProductName值Universal 360°Rotating Finger Ring Holder在文本文件中,导入后该值变为Universal360 旋转指环,与®,™等其他字符相同。



有没有与我的代码有关吗?



我尝试过:



 Public Function FillData(ByVal Fpath As String)As Boolean 
Dim XRead As System.IO.StreamReader = New IO.StreamReader(FilePath)
Dim XLine As String = Nothing
Dim XSplitLine()As String
Dim i As Integer = ImportedData.Rows.Count + 1
尝试
XRead.ReadLine()
XLine = XRead.ReadLine()
Do直到XLine什么都没有

XLine = i& vbTab& XLine& vbTab& FilePath
XSplitLine = XLine.Split(CType(vbTab,Char()))

ImportedData.Rows.Add(XSplitLine)
XLine = XRead.ReadLine
i + = 1
循环
XRead.Close()

Catch ex As Exception
MessageBox.Show(Error)
返回False
退出函数
结束尝试

返回True
结束函数

解决方案

< blockquote>我不同意解决方案1.来自 Encoding.Default属性(System.Text)| Microsoft Docs [ ^ ]



引用:

不同的计算机可以使用不同的编码作为默认编码,默认编码可以在一台计算机上更改。如果使用默认编码对计算机之间流式传输的数据进行编码和解码,或在同一台计算机上的不同时间检索这些数据,则可能会错误地转换该数据。此外,Default属性返回的编码使用最适合的回退将不受支持的字符映射到代码页支持的字符。由于这些原因,建议不要使用默认编码。要确保正确解码编码的字节,您应该使用Unicode编码,例如UTF8Encoding或UnicodeEncoding。您还可以使用更高级别的协议来确保使用相同的格式进行编码和解码。

我会明确地使用

  Dim  XRead  As  System.IO .StreamReader =  IO.StreamReader(FilePath,System.Text.Encoding.UnicodeEncoding)

有关详细信息,请参阅编码类(System.Text)| Microsoft Docs [ ^ ]


I am Using StreamReader to import data from Tab Delim Text file to a Data Table called Import Data. For some reasons none of the superScript characters are readable once its imported to the table.

For example If i have a ProductName value "Universal 360° Rotating Finger Ring Holder" in the text file, after import the value becomes like "Universal 360� Rotating Finger Ring Holder" It's same with other characters like " ® , ™ ".

Is there something to do with my code?

What I have tried:

Public Function FillData(ByVal Fpath As String) As Boolean
        Dim XRead As System.IO.StreamReader = New IO.StreamReader(FilePath)
        Dim XLine As String = Nothing
        Dim XSplitLine() As String
        Dim i As Integer = ImportedData.Rows.Count + 1
        Try
            XRead.ReadLine()
            XLine = XRead.ReadLine()
            Do Until XLine Is Nothing

                XLine = i & vbTab & XLine & vbTab & FilePath
                XSplitLine = XLine.Split(CType(vbTab, Char()))

                ImportedData.Rows.Add(XSplitLine)
                XLine = XRead.ReadLine
                i += 1
            Loop
            XRead.Close()

        Catch ex As Exception
            MessageBox.Show("Error")
            Return False
            Exit Function
        End Try

        Return True
    End Function

解决方案

I disagree with Solution 1. Quote from Encoding.Default Property (System.Text) | Microsoft Docs[^]

Quote:

Different computers can use different encodings as the default, and the default encoding can change on a single computer. If you use the Default encoding to encode and decode data streamed between computers or retrieved at different times on the same computer, it may translate that data incorrectly. In addition, the encoding returned by the Default property uses best-fit fallback to map unsupported characters to characters supported by the code page. For these reasons, using the default encoding is not recommended. To ensure that encoded bytes are decoded properly, you should use a Unicode encoding, such as UTF8Encoding or UnicodeEncoding. You could also use a higher-level protocol to ensure that the same format is used for encoding and decoding.

I would explicitly use

Dim XRead As System.IO.StreamReader = New IO.StreamReader(FilePath, System.Text.Encoding.UnicodeEncoding)

For more information see Encoding Class (System.Text) | Microsoft Docs[^]


这篇关于有人知道为什么流阅读器不能读取上标字符(VB.NET)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆