1

I have the following XML, and I'm trying to get into an array using LINQ:

<doc>
  <rows>
    <cols>
      <a>value</a>
      <b>value</b>
      <c>value</c>
      <d>value</d>
    </cols>
    // multiple col elements
 </rows>
 // multiple rows elements, each has a set of col elements
</doc>
// single doc (root) element

using this code:

ObjectType[][] var = (
from e in XDocument.Load("Test.xml").Root.Elements()
select (
from cols in e.Elements("cols")
select new ObjectType
{
a = (int)e.Element("a"),
b = (int)e.Element("b"),
c = (int)e.Element("c"),
d = (int)e.Element("d")
}).ToArray()).ToArray();

Where ObjectType is a simple structure of ints: a, b, c, and d. The XML looks properly-formatted, but I keep getting System.ArgumentNullException. From what I can make out in the debugger, it doesn't get past the first element.

How is my brain failing me on this one?

1 Answer 1

2

it should be like this

a = (int)cols.Element("a"),
b = (int)cols.Element("b"),
c = (int)cols.Element("c"),
d = (int)cols.Element("d")

use cols instead of e, e is for rows. also you have to change the word value in your xml to integer numbers

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

Comments

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.