Getting FileHelpers 2.0 to handle CSV files with excess commas

佐手、 提交于 2019-11-29 11:11:58

Just an idea for a hack / workaround: you could create a property called "ExtraCommas" and add it to your class, so that extra commas are serialized there and not in the real properties of your object...

If the number of commas varies, I think you are out of luck and would have to do post processing. However you can set blank fields in your class if there are a fixed amount.

[FieldOrder(5)] 
public string Blank1;

[FieldOrder(6)]
public string Blank2;

This doesn't really ever bite me because I don't use a FileHelpers class as a business class, I use it as an object to build the business class from. I store it for auditing. I think at one point I played around with making the fields for the Blanks private, not sure how that turned out.

Here is a custom method that you can use, it might not be the best solution, but it will solve the last comma problem. The code could be more optimized for sure, this is just to give you the idea of how to get around this kind of problem.

  int main(){
    StreamReader sr = new StreamReader(@"C:\Users\musab.shaheed\Desktop\csv.csv");
     var lineCount=File.ReadLines(@"C:\Users\musab.shaheed\Desktop\csv.csv").Count();
            for (int i = 0; i < lineCount;i++ ) {

            String fileText = sr.ReadLine();

            fileText=fileText.Substring(0, fileText.Length - 1);


            //store your data in here
            Console.WriteLine(fileText);


            };

            sr.Close();

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!