ReadLine的替代品?

时间:2014-01-30 19:19:10

标签: c# streamreader readline

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

我无法替换这些,因为我需要在过程后检查它,所以我需要获得分隔线和之后的内容。那就是问题所在。我怎么能这样做?

这是我的代码:

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;
    }

1 个答案:

答案 0 :(得分:1)

如果您正在尝试阅读XML,请尝试使用内置的库,这是一个加载一段带有<TopLevelTag>的XML的简单示例。

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

这是一种获取内容的简洁方法,如果从XML中丢失则会抛出异常。

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

如果您真的需要自己动手,那么请查看CSV解析器的示例, 其中ReadBlock可用于获取原始数据,包括换行符。

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);