<p>I need to read from a variety of different text files (I've some delimited files and some fixed width files). I've considered parsing the files line by line (slow using the File.ReadLine type methods) and reading the file using the ODBC text driver (faster) but does anyone have any other (better) suggestions? I'm using .NET/C#.</p>

[{'answer_id': 34190, 'author': 'jfs', 'author_id': 718, 'author_profile': 'https://Stackoverflow.com/users/718', 'pm_score': 1, 'selected': False, 'text': '<p>If the files are relatively small you can use the <a href="http://msdn.microsoft.com/en-us/library/system.io.file.aspx" rel="nofollow noreferrer">File</a> class. It has these methods which may help you:</p>\n\n<ul>\n<li>ReadAllBytes</li>\n<li>ReadAllLines</li>\n<li>ReadAllText  </li>\n</ul>\n'}, {'answer_id': 34192, 'author': 'Martin Marconcini', 'author_id': 2684, 'author_profile': 'https://Stackoverflow.com/users/2684', 'pm_score': 0, 'selected': False, 'text': "<p>The File reading process is not slow if you read all file at once using the File class and the methods suggested by John. Depending upon the file's size and what you want to do with them, it may use more or less memory. I'd suggest you try with File.ReadAllText (or whatever is appropriate for you)</p>\n"}, {'answer_id': 34196, 'author': 'Mike Thompson', 'author_id': 2754, 'author_profile': 'https://Stackoverflow.com/users/2754', 'pm_score': 1, 'selected': False, 'text': '<p>Your question is a little vague. I assume that the text files contain structured data, not just random lines of text. </p>\n\n<p>If you are parsing the files yourself then .NET has a library function to read all the lines from a text file into an array of strings (File.ReadAllLines). If you know your files are small enough to hold in memory, then you can use this method and iterate over the array using a regular expression to validate &amp; extract the fields.</p>\n\n<p>Excel files are a different ball game. .XLS files are binary, not text, so you would need to use a 3rd party library to access them. .XLSX files from Excel 2007 contain compressed XML data, so once again you would need to decompress the XML then use an XML parser to get at the data. I would not recommend writing your own XML parser, unless you feel the need for the intellectual exercise.</p>\n'}, {'answer_id': 34201, 'author': 'Jon Limjap', 'author_id': 372, 'author_profile': 'https://Stackoverflow.com/users/372', 'pm_score': 3, 'selected': False, 'text': '<p>I\'m not sure you could really do a text-and-Excel file parser, not unless by Excel file you mean a comma/pipe/tab delimited file, which is actually just another text file. Reading actual excel files require you to use the MS Office libraries.</p>\n\n<p>For delimited text file parsing, you could look into <a href="http://www.filehelpers.com/" rel="nofollow noreferrer">FileHelpers</a> -- open source and they pretty much have it covered. Not sure if it will match your speed requirements though.</p>\n'}, {'answer_id': 34210, 'author': 'Martin Marconcini', 'author_id': 2684, 'author_profile': 'https://Stackoverflow.com/users/2684', 'pm_score': 0, 'selected': False, 'text': '<p>Regarding reading XLS Files: </p>\n\n<p>If you have Microsoft Office XP and above, you have access to the already included .NET SDK Office Libraries, where you can "natively" read XLS files, Word, PPT, etc. Please note that under Office XP you have to manually check that during install (unless you had .NET previously installed). </p>\n\n<p>I don\'t know if these libraries are available as a separate package if you don\'t have Microsoft Office. </p>\n\n<p>For some obscure reason, all these libraries (including the latest versions from Office 2007 -a.k.a.: Office 12), are COM components that are a <em>pain</em> to use, cause ugly dependencies and are <strong>not</strong> backwards compatible. I.E.: if you have some methods that work with Office XP (Office11), and you install that onto a customer with Office 12, it <em>doesn\'t work</em>, because some interfaces where changed. So you need to maintain <em>two</em> set of "libraries" and methods to deal with that. The same holds true if use Office 12 libraries to program, and you customer has Office 11. Your libraries don\'t work. :S</p>\n\n<p>I don\'t know why Microsoft never created a Microsoft.Office.XXXX managed library (wrapper) around those ugly things. </p>\n\n<p>Anyways, your question is quite strange, try to follow some advice here. Good luck!</p>\n'}, {'answer_id': 34216, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>Ignoring the Excel part (which you say isn\'t important):</p>\n\n<p>I\'ve found LINQ to be fairly useful in parsing txt files (pipe-delimited or csv)</p>\n\n<p>e.g. This reads a pipe-delimited file skipping the hader row and creates an IEnumerable as the result:</p>\n\n<p>var records =\n                from line in File.ReadAllLines(@"c:\\blah.txt").Skip(1)\n                let parts = line.Split(\'|\')\n                select parts;</p>\n'}, {'answer_id': 34248, 'author': 'Keith', 'author_id': 905, 'author_profile': 'https://Stackoverflow.com/users/905', 'pm_score': 0, 'selected': False, 'text': "<p>The ODBC text driver is now rather out of date - it has no Unicode support.</p>\n\n<p>Amazingly MS Excel still uses it, so if you <em>open</em> a Unicode CSV in Excel 2007 (rather than import it) you lose all non-ASCII chars.</p>\n\n<p>You best bet is to use .Net's file reading methods, as others have suggested.</p>\n"}, {'answer_id': 34250, 'author': 'ljs', 'author_id': 3394, 'author_profile': 'https://Stackoverflow.com/users/3394', 'pm_score': 1, 'selected': False, 'text': '<p>I agree with John,</p>\n\n<p>For example:-</p>\n\n<pre><code>using System.IO;\n\n...\n\npublic class Program {\n  public static void Main() {\n    foreach(string s in File.ReadAllLines(@"c:\\foo\\bar\\something.txt") {\n      // Do something with each line...\n    }\n  }\n}\n</code></pre>\n'}, {'answer_id': 56934, 'author': 'Graham', 'author_id': 1826, 'author_profile': 'https://Stackoverflow.com/users/1826', 'pm_score': 3, 'selected': True, 'text': '<p>Answering my own question:</p>\n\n<p>I ended up using the Microsoft.VisualBasic.FileIO.TextFieldParser object, see:</p>\n\n<p><a href="http://msdn.microsoft.com/en-us/library/f68t4563.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/f68t4563.aspx</a></p>\n\n<p><s><a href="http://www.siccolo.com/Articles/CodeProject/Open_DataSet_From_TextFile/open_dataset_from_text_csv_file.html" rel="nofollow noreferrer">(example of implementation here)</a></s></p>\n\n<p>This allows me to handle csv files without worrying about how to cope with whether fields are enclosed in quotes, contain commas, escaped quotes etc.</p>\n'}]
['https://Stackoverflow.com/questions/34182', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1826/']