1

Initializing a jagged array in C# from an XML file. The following code seems to work to create the data structure I want, from what I can see in the debugger, but the code returns a value of type IEnumerable.

var ia = (from e in XDocument.Load("Test.xml").Descendants("doc") select ( from rows in e.Elements("rows") select ( from cols in rows.Elements("cols") select int.Parse(cols.Value) ).ToArray() ).ToArray()); 

If I throw in an extra .ToArray() on the end (thanks to other StackOverflow articles), I get back int[1][][].

Any ideas on how to get the code working so I can simply write int[][] array = (from e in... etc.?

2
  • you have descendants * rows * cols. Thats 3D, and you want 2D. That's the real problem. My guess is you have just a single doc, right? Commented Jan 9, 2014 at 0:40
  • You have three levels of enumeration, so there will be three levels of depth in the jagged array. You can have multiple "docs", which can have multiple "rows", which can have multiple "cols". Which arrays do you want to combine so that you only have two levels? Commented Jan 9, 2014 at 0:40

2 Answers 2

1

If there is always only one doc element you can do following:

var ia = (from e in XDocument.Load("Test.xml").Descendants("doc") select ( from rows in e.Elements("rows") select ( from cols in rows.Elements("cols") select int.Parse(cols.Value) ).ToArray() ).First().ToArray()); 

If you're expecting more then one doc element you can make additional SelectMany call:

var ia = (from e in XDocument.Load("Test.xml").Descendants("doc") select ( from rows in e.Elements("rows") select ( from cols in rows.Elements("cols") select int.Parse(cols.Value) ).ToArray() ).SelectMany(x => x).ToArray()); 
Sign up to request clarification or add additional context in comments.

5 Comments

<hits head> Yes, only one doc element. I guess I should re-think using Descendants in this context. Thanks for the fix, everyone. You guys came back so quickly...!
@Yes, you should definitely think about not using Descendants like that. Why (or when) you should/shouldn't use Descendants() method
Good article, Marcin. Thanks!
For anyone reading this in future, here's the final code that worked, using Marcin's article: int[][] ia = (from e in XDocument.Load("Test.xml").Element("doc").Elements("rows") select ( from cols in e.Elements("cols") select int.Parse(cols.Value)).ToArray()).ToArray();
If doc is the root element on your document you can change Load().Element("doc").Elements() to Load().Root.Elements()
0

You have descendants * rows * cols. Thats 3D, and you want 2D. I guess you expect just a single "doc" descendant in your xml file. You can verify that with Single which fails when there's more than one item in the collection, meaning more than one "doc":

var ia = (from e in XDocument.Load("Test.xml").Descendants("doc") select ( from rows in e.Elements("rows") select ( from cols in rows.Elements("cols") select int.Parse(cols.Value)).ToArray()).Single().ToArray()); 

1 Comment

Thanks - will check this out, too, i3arnon.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.