I have a csv file (comma delimited) that contains an unknown number of columns, and unkown number of rows, and an unknown size of string for each element. Each row has the same number of elements however. I desire to read this data into a multidimensional array or List. Because I do not now the size a List seems most appropriate, but I am having difficulties.

I can read each line into a null string and separate the entries and create a single dimensional array for each row that is read in thus;

List<string> fileLines = File.ReadAllLines(strFileName).ToList();
int entries = fileLines.Count;
string str = String.Empty;
List<string> values = new List<string>();
for (i = 0; i < entries; i++){
     str = fileLines[i];//contains the value of the entire row in one string
     a = str.Split(',');//broken up into its elements.                              
     for (j=1; j<a.Length; j++){//need to eliminate the first element
          c = a[j];
          values.Add(c);//List that contains the elements of a that I need.

Now I need to assign each 'value' to a row in a multidimensional list, but I cant figure out how to do that. It seems that sring arrays and Lists do not have the same method sets. The string array method set would work, but I would have to initialize the array size, and I cant do that because I dont know it.

Edited by __avd: Added [code] tags.

7 Years
Discussion Span
Last Post by __avd

This code uses the DataTable class, which I am not familiar with, but seems it could work for me. I was hoping to be able to use a read two dimensional List:

List<string[,]> table = new List<string[,]>();

where each row in the 2-D list contains a 1-D string List already obtained.


Well this line, which reads just one line of the file and drops all the string in a fieldValues array

string[] fieldValues = myReader.ReadLine().Split(new Char [] {seperator});

should give you some ideas, fieldValues.Length; tels you how many columns you have in your csv file.

Edited by ddanbe: n/a


As you say, because I know that the number of items in each row is constant, and I can count the number of rows and number of columns when I read in the file, I can create a multi-dimensional array thus;

string[,] c = new string[a,b];

And it works.

what doesnt work is a multi-dimensional List or a string array defined as;
string[][] c = new string[a];//not allowed even if a and b are known

any ideas why string[][] doesnt work and why a multidimensional List is such a hassle?



List<List<string>> lst = new List<List<string>>();

            List<string> a = new List<string>() { "One", "Two" };
            List<string> b = new List<string>() { "Three", "Four", "Five" };


             //Iterate list
            foreach (var t in lst)
                foreach (var ele in t)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.