Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to show the index of field when MalformedLineException occurs just like the Line number

input file like
a,b,c
d,"e",f -- exception throws will show line number-2
gh,i,j

C#
using System;
using Microsoft.VisualBasic.FileIO;

class Program
{
    static void Main()
    {
       try
{
	using (TextFieldParser parser = new TextFieldParser("C:\\csv.txt"))
	{
	    parser.Delimiters = new string[] { "," };
	    while (true)
	    {
		string[] parts = parser.ReadFields();
		if (parts == null)
		{
		    break;
		}
		Console.WriteLine("{0} field(s)", parts.Length);
	    }
	}
      catch (&MalformedLineException ex)
                {
                    throw new Exception("Malformed Line Exception in GetGenericFields at line number : " + ex.LineNumber.ToString() + "</br> with filepath :" + localPath.ToString() + "</br> and reason is :" + ex.Message.ToString() + "</br> and inner stack trace :" + ex.StackTrace.ToString());
                }
    }
}
}


What I have tried:

MalformedLineException will show line number but not column index
Posted
Comments
[no name] 11-Feb-16 1:17am    
As per your code you should get LineNumber. Secondly did you check the stacktarce information of exception.

You can declare a counter variable and increase its value to 1 inside loop. When exceptions occurs you will get the counter value as Line number.
dan!sh 11-Feb-16 1:28am    
You will need to create your own parser or extend the existing parser. The current implementation does not provide that information.

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