c# - 使用 UTF-8 解码文件流

标签 c# validation encoding utf-8

我有一个 XML 文档,它非常大(大约 120M),我不想立即将其加载到内存中。我的目的是检查此文件是否使用有效的 UTF-8 编码。

在不将整个文件以 byte[] 的形式读入内存的情况下进行快速检查的任何想法?

我正在使用 VSTS 2008 和 C#。

使用时 XMLDocument加载一个包含无效字节序列的 XML 文档,有一个异常(exception),但是当将所有内容读入一个字节数组然后检查 UTF-8 时,没有任何异常(exception),有什么想法吗?

这是显示我的 XML 文件内容的屏幕截图,或者您可以从 here 下载该文件的副本。

enter image description here

编辑 1:

class Program
{
    public static byte[] RawReadingTest(string fileName)
    {
        byte[] buff = null;

        try
        {
            FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            BinaryReader br = new BinaryReader(fs);
            long numBytes = new FileInfo(fileName).Length;
            buff = br.ReadBytes((int)numBytes);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

        return buff;
    }

    static void XMLTest()
    {
        try
        {
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load("c:\\abc.xml");
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }

    static void Main()
    {
        try
        {
            XMLTest();
            Encoding ae = Encoding.GetEncoding("utf-8");
            string filename = "c:\\abc.xml";
            ae.GetString(RawReadingTest(filename));
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

        return;
    }
}

编辑 2:使用时 new UTF8Encoding(true, true)会有异常(exception),但是当使用 new UTF8Encoding(false, true) 时,没有抛出异常。我很困惑,因为它应该是控制是否抛出异常的第二个参数(如果有无效的字节序列),为什么第一个参数很重要?
    public static void TestTextReader2()
    {
        try
        {
            // Create an instance of StreamReader to read from a file.
            // The using statement also closes the StreamReader.
            using (StreamReader sr = new StreamReader(
                "c:\\a.xml",
                new UTF8Encoding(true, true)
                ))
            {
                int bufferSize = 10 * 1024 * 1024; //could be anything
                char[] buffer = new char[bufferSize];
                // Read from the file until the end of the file is reached.
                int actualsize = sr.Read(buffer, 0, bufferSize);
                while (actualsize > 0)
                {
                    actualsize = sr.Read(buffer, 0, bufferSize);
                }
            }
        }
        catch (Exception e)
        {
            // Let the user know what went wrong.
            Console.WriteLine("The file could not be read:");
            Console.WriteLine(e.Message);
        }

    }

最佳答案

var buffer = new char[32768] ;

using (var stream = new StreamReader (pathToFile, 
    new UTF8Encoding (true, true)))
{
    while (true)
    try
    {
        if (stream.Read (buffer, 0, buffer.Length) == 0)
            return GoodUTF8File ;
    }
    catch (ArgumentException)
    {
        return BadUTF8File ;
    }
}

关于c# - 使用 UTF-8 解码文件流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/876399/

相关文章:

django - 如何实现投票验证系统?

c - 以宽字符作为索引的全局 C 数组

python - Python中的Url解码UTF-8

node.js - 如何在 Node.js 中进行 Base64 编码?

c# - 将二维数组的二维数组转换为单个二维数组

c# - 如何在格式字符串中设置占位符的颜色

c# - Java语言转换助手和J#现状

css - 设置 Angular 验证的最小日期

c# Appsettings 给出错误 : The name 'ConfigurationManager' does not exist in the current context

reactjs - 如何使用 yup.addMethod() 为国家/地区名称和代码编写自定义架构验证?