在 C# 中读取 CSV 文件的编码字符串问题
Encoding string issue reading a CSV file in C#
我目前正在开发一个 Windows Phone 8 应用程序,其中一个我必须从网络服务下载 CSV 文件并将数据转换为 C# 业务对象(我不使用这部分的库)。
使用 RestSharp.Portable、StreamReader
class 和 MemoryStream
class 下载文件并将数据转换为 C# 业务对象不是问题。
我遇到的问题是字符串字段的错误编码。
使用库 RestSharp.Portable,我将 csv 文件内容检索为字节数组,然后使用以下代码将数据转换为字符串(其中 response
是字节数组):
using (var streamReader = new StreamReader(new MemoryStream(response)))
{
while (streamReader.Peek() >= 0)
{
var csvLine = streamReader.ReadLine();
}
}
但我的 csvLine
变量包含 J�rome
而不是 "Jérome"。我尝试了几件事来获得 Jérome
但没有成功,例如:
using (var streamReader = new StreamReader(new MemoryStream(response), true))
或
using (var streamReader = new StreamReader(new MemoryStream(response), Encoding.UTF8))
当我使用像 notepad++ 这样的简单记事本软件打开 CSV 文件时,只有当文件以 ANSI 编码时,我才会获得 Jérome
。但是如果我在 C# 中尝试以下代码:
using (var streamReader = new StreamReader(new MemoryStream(response), Encoding.GetEncoding("ANSI")))
我有以下异常:
'ANSI' is not a supported encoding name.
谁能帮我正确解码我的 CSV 文件?
提前感谢您的帮助或建议!
您需要选择其中一项。
https://msdn.microsoft.com/en-us/library/windows/desktop/dd317756(v=vs.85).aspx
不知道的可以试试猜猜。根据答案 here.
,猜测并不是一个完美的解决方案
You can't detect the codepage, you need to be told it. You can analyse the bytes and guess it, but that can give some bizarre (sometimes amusing) results.
来自 Lawtonfogle 的 link 我尝试使用
using (var streamReader = new StreamReader(new MemoryStream(response), Encoding.GetEncoding("Windows-1252")))
但是我遇到了以下错误:
'Windows-1252' is not a supported encoding name.
在互联网上搜索原因,我终于找到了适合我的 thread with the following answer。
所以这里是我的工作解决方案:
using (var streamReader = new StreamReader(new MemoryStream(response), Encoding.GetEncoding("ISO-8859-1")))
{
while (streamReader.Peek() >= 0)
{
var csvLine = streamReader.ReadLine();
}
}
我目前正在开发一个 Windows Phone 8 应用程序,其中一个我必须从网络服务下载 CSV 文件并将数据转换为 C# 业务对象(我不使用这部分的库)。
使用 RestSharp.Portable、StreamReader
class 和 MemoryStream
class 下载文件并将数据转换为 C# 业务对象不是问题。
我遇到的问题是字符串字段的错误编码。
使用库 RestSharp.Portable,我将 csv 文件内容检索为字节数组,然后使用以下代码将数据转换为字符串(其中 response
是字节数组):
using (var streamReader = new StreamReader(new MemoryStream(response)))
{
while (streamReader.Peek() >= 0)
{
var csvLine = streamReader.ReadLine();
}
}
但我的 csvLine
变量包含 J�rome
而不是 "Jérome"。我尝试了几件事来获得 Jérome
但没有成功,例如:
using (var streamReader = new StreamReader(new MemoryStream(response), true))
或
using (var streamReader = new StreamReader(new MemoryStream(response), Encoding.UTF8))
当我使用像 notepad++ 这样的简单记事本软件打开 CSV 文件时,只有当文件以 ANSI 编码时,我才会获得 Jérome
。但是如果我在 C# 中尝试以下代码:
using (var streamReader = new StreamReader(new MemoryStream(response), Encoding.GetEncoding("ANSI")))
我有以下异常:
'ANSI' is not a supported encoding name.
谁能帮我正确解码我的 CSV 文件?
提前感谢您的帮助或建议!
您需要选择其中一项。
https://msdn.microsoft.com/en-us/library/windows/desktop/dd317756(v=vs.85).aspx
不知道的可以试试猜猜。根据答案 here.
,猜测并不是一个完美的解决方案You can't detect the codepage, you need to be told it. You can analyse the bytes and guess it, but that can give some bizarre (sometimes amusing) results.
来自 Lawtonfogle 的 link 我尝试使用
using (var streamReader = new StreamReader(new MemoryStream(response), Encoding.GetEncoding("Windows-1252")))
但是我遇到了以下错误:
'Windows-1252' is not a supported encoding name.
在互联网上搜索原因,我终于找到了适合我的 thread with the following answer。
所以这里是我的工作解决方案:
using (var streamReader = new StreamReader(new MemoryStream(response), Encoding.GetEncoding("ISO-8859-1")))
{
while (streamReader.Peek() >= 0)
{
var csvLine = streamReader.ReadLine();
}
}