替代 ReadLine? [英] Alternative to ReadLine?

查看:67
本文介绍了替代 ReadLine?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 ReadLine 读取一些文件,但是我的文件有一些我需要捕捉的断行(不是全部),我不知道如何获取它们在同一个数组中,没有在任何其他具有这些分隔符的数组中...因为... ReadLine 读取行,并打破这些行,是吧?

I'm trying to read some files with ReadLine, but my file have some break lines that I need to catch (not all of them), and I don't know how to get them in the same array, neither in any other array with these separators... because... ReadLine reads lines, and break these lines, huh?

我无法替换这些,因为我需要在此过程后对其进行检查,因此我需要在此之后获取特征线和内容.那就是问题所在.我该怎么做?

I can't replace these because I need to check it after the process, so I need to get the breaklines AND the content after that. That's the problem. How can I do that?

这是我的代码:

public class ReadFile
{
    string extension;
    string filename;
    System.IO.StreamReader sr;

    public ReadFile(string arquivo, System.IO.StreamReader sr)
    {
        string ext = Path.GetExtension(arquivo);

        sr = new StreamReader(arquivo, System.Text.Encoding.Default);

        this.sr = sr;
        this.extension = ext;
        this.filename = Path.GetFileNameWithoutExtension(arquivo);

        if (ext.Equals(".EXP", StringComparison.OrdinalIgnoreCase))
        {
            ReadEXP(arquivo);
        }
        else MessageBox.Show("Extensão de arquivo não suportada: "+ext);

    }

    public void ReadEXP(string arquivo)
    {

        string line = sr.ReadLine();

        string[] words;
        string[] Separators = new string[] { "<Segment>", "</Segment>", "<Source>", "</Source>", "<Target>", "</Target>" };

        string ID = null;
        string Source = null;
        string Target = null;

        DataBase db = new DataBase();
        //db.CreateTable_EXP(filename);
        db.CreateTable_EXP();

        while ((line = sr.ReadLine()) != null)
        {

            try
            {

                if (line.Contains("<Segment>"))
                {
                    ID = "";
                    words = line.Split(Separators, StringSplitOptions.None);
                    ID = words[0];
                    for (int i = 1; i < words.Length; i++ )
                        ID += words[i];
                    MessageBox.Show("Segment[" + words.Length + "]: " + ID);
                }
                if (line.Contains("<Source>"))
                {
                    Source = "";
                    words = line.Split(Separators, StringSplitOptions.None);
                    Source = words[0];
                    for (int i = 1; i < words.Length; i++)
                        Source += words[i];
                    MessageBox.Show("Source[" + words.Length + "]: " + Source);
                }
                if (line.Contains("<Target>"))
                {
                    Target = "";
                    words = line.Split(Separators, StringSplitOptions.None);
                    Target = words[0];
                    for (int i = 1; i < words.Length; i++)
                        Target += words[i];
                    MessageBox.Show("Target[" + words.Length + "]: " + Target);

                    db.PopulateTable_EXP(ID, Source, Target);

                    MessageBox.Show("ID: " + ID + "\nSource: " + Source + "\nTarget: " + Target);
                }

            }
            catch (IndexOutOfRangeException e)
            {
                MessageBox.Show(e.Message.ToString());
                MessageBox.Show("ID: " + ID + "\nSource: " + Source + "\nTarget: " + Target);
            }

        }

        return;
    }

推荐答案

如果您正在尝试读取 XML,请尝试使用内置库,这里是一个使用 加载 XML 部分的简单示例; 在里面.

If you are trying to read XML, try using the built in libaries, here is a simple example of loading a section of XML with <TopLevelTag> in it.

var xmlData = XDocument.Load(@"C:\folder\file.xml").Element("TopLevelTag");
if (xmlData == null) throw new Exception("Failed To Load XML");

这是一种获取内容的整洁方法,如果 XML 中缺少内容,它不会抛出异常.

Here is a tidy way to get content without it throwing an exception if missing from the XML.

var xmlBit = (string)xmlData.Element("SomeSubTag") ?? "";

如果你真的必须自己动手,那么看看 CSV 解析器的例子,其中 ReadBlock 可用于获取包括换行符在内的原始数据.

If you really have to roll your own, then look at examples for CSV parsers, where ReadBlock can be used to get the raw data including line breaks.

private char[] chunkBuffer = new char[4096];
var fileStream = new System.IO.StreamReader(new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite));
var chunkLength = fileStream.ReadBlock(chunkBuffer, 0, chunkBuffer.Length);

这篇关于替代 ReadLine?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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