在我们的应用中,我们收到的文本文件(.txt,.csv从不同的来源,等等).在阅读时,这些文件有时会包含垃圾,因为文件是在不同的/未知的代码页中创建的.
有没有办法(自动)检测文本文件的代码页?
的detectEncodingFromByteOrderMarks,对StreamReader构造,工程UTF8 和其他的Unicode标文件,但是我正在寻找一种方法来检测代码页,像ibm850,windows1252.
谢谢你的回答,这就是我所做的.
我们收到的文件来自最终用户,他们没有关于代码页的线索.接收者也是最终用户,到目前为止,这是他们对代码页的了解:代码页存在,并且令人讨厌.
解:
这个程序在为.NET 4编译时工作正常,但在为.NET Core编译时也能正常工作.我理解不支持编码的错误,但不知道如何解决它.
Public Class Program
Public Shared Function Main(ByVal args As String()) As Integer
System.Text.Encoding.GetEncoding(1252)
End Function
End Class
Run Code Online (Sandbox Code Playgroud) 我错过了什么或System.IO.FileStream不读取包含希伯来语的Unicode文本文件?
public TextReader CSVReader(Stream s, Encoding enc)
{
this.stream = s;
if (!s.CanRead)
{
throw new CSVReaderException("Could not read the given CSV stream!");
}
reader = (enc != null) ? new StreamReader(s, enc) : new StreamReader(s);
}
Run Code Online (Sandbox Code Playgroud)
谢谢乔纳森
我有一个file.xml用Iso-latin-15(又名Iso-Latin-9)编码的XML文档
<?xml version="1.0" encoding="iso-8859-15"?>
<root xmlns="http://stackoverflow.com/demo">
<f>€.txt</f>
</root>
Run Code Online (Sandbox Code Playgroud)
从我最喜欢的文本编辑器,我可以告诉这个文件在Iso-Latin-15中正确编码(它不是UTF-8).
我的软件是用C#编写的,想要提取元素f.
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("file.xml");
Run Code Online (Sandbox Code Playgroud)
在现实生活中,我有一个XMLResolver来设置凭据.但基本上,我的代码就是这么简单.装载进展顺利,我没有任何例外.
现在,我提取值时的问题:
//xnsm is the XmlNameSpace manager
XmlNode n = xmlDoc.SelectSingleNode("//root/f", xnsm);
if (n != null)
String filename = n.InnerText;
Run Code Online (Sandbox Code Playgroud)
Visual Studio调试器显示filename = ?.txt
它可能只是一个Visual Studio错误.不幸的是File.Exists(filename)返回false,而文件实际存在.
怎么了?