Click here to Skip to main content
15,896,398 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to read text file skipping white space in asp.net with c#. I am reading the text file using stream reader.

Code is as below:
C#
using (StreamReader sr = new StreamReader(CSVFilePathName))
{
	while (!sr.EndOfStream)
	{
		if (sr != null)
		{
		   
			strData = File.ReadLines(CSVFilePathName).ElementAt(lineno).Split(',');
		}
	
	}
}


What I have tried:

Code is as below:
<pre> using (StreamReader sr = new StreamReader(CSVFilePathName))
                {
                    while (!sr.EndOfStream)
                    {
                                                
                        if (sr != null)
                        {
                           
                            strData = File.ReadLines(CSVFilePathName).ElementAt(lineno).Split(',');
}
Posted
Updated 30-Mar-20 5:56am
v2
Comments
Richard MacCutchan 30-Mar-20 11:14am    
What is the problem?
Richard Deeming 1-Apr-20 14:26pm    
Other than opening the file for reading twice, and the infinite loop caused by never advancing the StreamReader? :)

1 solution

Try
C#
using (StreamReader sr = new StreamReader(CSVFilePathName))
{
   string line;
   while ((line = sr.ReadLine()) != null)
   {
      strData = line.Split(',');
      for (int i = 0; i < strData.Length; ++i)
      {
         strData[i] = strData[i].Trim();
      }
      // ...
   }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900