Mokmeuh
Mokmeuh

Reputation: 885

Load xml file into 2d rectangular array using linq

I'm currently using a jagged array : private double[][] array; and I load the array like so.

Loading the jagged array

XDocument doc = XDocument.Load("file.Xml");
array = doc.Root.Elements("Month").Select(month => month.Elements().Select(x => (double)x).ToArray()).ToArray();

So now my problem is I need to get the lenght of the inner array and as I readed it's not possible, so I would need to load the xml in a rectangular array private double[,] array; but with linq I don't know how.

XML looks

<document>
  <Month>
    <Depth>-0.25</Depth>
    <October>0.95</October>
    <November>-0.90</November>
    ...
  </Month>
  <Month>
    <Depth>-0.5</Depth>
    <October>0.47</October>
    <November>-0.17</November>
    ...
  </Month>
  ...
</document>

Upvotes: 2

Views: 490

Answers (1)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236278

double[][] array = 
   doc.Root.Elements("Month")
      .Select(month => month.Elements().Select(x => (double)x).ToArray())
      .ToArray();

foreach(var innerArray in array)
    Console.WriteLine(innerArray.Length); // you can get length of inner array

Upvotes: 2

Related Questions