1

I'm using the C# CLosedXML package to acquire XML data values and place them into a list. From what I found online, the only example of storing the values into an array in a list is as shown below.

var dataList = new List<string[]>()
{
    table.DataRange.Rows().Select(tableRow => tableRow.Field("Story").GetString()).ToArray(),
    table.DataRange.Rows().Select(tableRow => tableRow.Field("Level").GetString()).ToArray(),
    table.DataRange.Rows().Select(tableRow => tableRow.Field("Combo").GetString()).ToArray(),
    table.DataRange.Rows().Select(tableRow => tableRow.Field("Location").GetString()).ToArray(),
    table.DataRange.Rows().Select(tableRow => tableRow.Field("Asset").GetString()).ToArray(),
}

I have tried to print the details out in a console to visualize if the data was correctly stored in the list array which I've created. However, the dataList count is 5. Is it caused by the array size and is there a better way of coding this?

for (int i = 0; i < dataList.Count ; i++)
{
    for (int j = 0; j < totalRows; j++)//totalRows more than 10
    {
        Console.WriteLine("Column {0} details: {1}", i+1, dataList[i][j]);
    }
}

Thank you!

AbdelAziz AbdelLatef
  • 3,650
  • 6
  • 24
  • 52
Nate
  • 119
  • 1
  • 2
  • 10

2 Answers2

1

You are explicitly initializing the dataList variable as a list of 5 elements, each of which is an array of strings.

Francois Botha
  • 4,520
  • 1
  • 34
  • 46
  • Thank you Francois. I have trouble with the code as the additional data in each element cant be stored of more than 5. I believe its due to this error that I am getting _System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')'_. In my excel file, I have more than 10 additional data which needs to be stored in the array. Could you advise where I went wrong with the script and how I can include the additional data? – Nate Oct 23 '19 at 03:38
0

You can use a foreach like:

int index = 0;
foreach (string[] item in dataList)       
 {
   for (int i = 0; i < item.Length; i++)
   {
      Console.WriteLine("Column {0} details: {1}", index+1, item[i]);
   }
        index++;
}