ReadLine的替代品?(Alternative to ReadLine?)

编程入门 行业动态 更新时间:2024-10-24 20:23:29
ReadLine的替代品?(Alternative to 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; }

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?

Here's my code:

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,请尝试使用内置库,这是一个使用<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);

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

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

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

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

更多推荐

本文发布于:2023-08-04 11:01:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1415143.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:替代品   ReadLine   Alternative

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!