0

Like in title I have problem with parsing data from CVS files. When i choose file with diffrent formating all i get is "Input string was not in a correct format". My code works with files formatted like that:

16.990750 4.0
17.000250 5.0
17.009750 1.0
17.019250 6.0

But cant handle files formatted like this one:

Series1 - X;Series1 - Y;
285.75;798
285.79;764
285.84;578
285.88;690

This is code responsibile for reading data from file and creating chart from it:

if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            string cos = File.ReadAllText(openFileDialog1.FileName);
            string[] rows = cos.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);

            DataTable table = new DataTable();
            table.Columns.Add("xValue", typeof(decimal));
            table.Columns.Add("yValue", typeof(decimal));

            foreach (string row in rows)
            {
                string[] values = row.Split(' ');
                DataRow ch = table.NewRow();
                ch[0] = Decimal.Parse(values[0], NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture);
                ch[1] = Decimal.Parse(values[1], NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture);
                table.Rows.Add(ch);

            }
            if (seria == false)
            {
                wykres.Series.Add("series");
                wykres.Series["series"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
                wykres.Series["series"].XValueMember = "xValue";
                wykres.Series["series"].YValueMembers = "yValue";
                wykres.DataSource = table;
                wykres.DataBind();

                seria = true;



            }
         }

EDIT

I changed parsing method to this one:

                foreach (string row in rows)
            {
                var values = row.Split(';');
                var ch = table.NewRow();
                decimal num = 0;
                if (decimal.TryParse(values[0], out num))
                    ch[0] = num;
                if (decimal.TryParse(values[1], out num))
                    ch[1] = num;
                table.Rows.Add(ch);
            }

It works okay but with one exception - It can't read decimals only integers from csv file(see the picture below).

View of table in locals

Why is this happening?

4
  • Just change that one line that splits the row on spaces to splitting it on semi-colons: string[] values = row.Split(';'); Commented Jan 10, 2017 at 0:00
  • joshclose.github.io/CsvHelper Commented Jan 10, 2017 at 0:03
  • did u look with a debugger at the content of values after splitting the line Commented Jan 10, 2017 at 0:05
  • Btw. you can use File.ReadAllLines() to split the content already while reading it. Commented Jan 10, 2017 at 10:36

2 Answers 2

1

I suggest you don't re-invent the wheel, but use some well-tested library to parse the CSV (for example, your implementation does not handle quoted values well. It also doesn't allow the separator as part of a value).

And guess what: .NET includes something that could help you: the TextFieldParser class. Don't worry about the VisualBasicnamespace - it works in C#, too :-)

Sign up to request clarification or add additional context in comments.

Comments

0
foreach (string row in rows)
            {
                var values = row.Split(';');
                var ch = table.NewRow();
                decimal num = 0;
                if (decimal.TryParse(values[0], out num))
                    ch[0] = num;
                if (decimal.TryParse(values[1], out num))
                    ch[1] = num;
                table.Rows.Add(ch);
            }

In the second text format the separator is(;) and first row of the text has two strings therefore to convert a string to decimal use decimal.TryParse() instead of decimal.Parse().the return type of method TryParse() is boolean therefore if it returned true that means the string converted successful .

2 Comments

Add some explanation with answer for how this answer help OP in fixing current issue
@CodeCaster I have problem with this solution. I don't know why but every integer is parsed from file properly but decimals are ignored. image of what is kept in table from locals.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.