tags:

views:

61

answers:

1

Hi,

I have this XML:

<rootCategories>
    <category id="1">
        <category id="2">
            <category id="3">
                <category id="4" />
                <category id="5" />
                <category id="6" />
            </category>
            <category id="7" />
        </category>
    </category>
</rootCategories>

And I have this LINQ statement:

int count = doc.XPathSelectElements("//category").Elements().Count();

What I want is all of the "category" elements, basicaly, flattened into an array I can then foreach over.

The problem is that the count value returned (and, if that is knocked off, the array returned) shows that the top-most category elements are not being returned. In this case I'm getting 6 "category" elements returned, not 7.

I was using the XPath learnt from the this page: http://www.developer.com/xml/article.php/10929_3383961_1/NET-and-XML-XPath-Queries.htm

Could someone tell me why and how to fix this please? Is it something wrong in the XPath query?

Thanks,

Matt.

+6  A: 

Ok, this proves that a good night's sleep is the answer to most problems (or, at least, mine) as the solution is simply to remove the:

.Elements()

from the LINQ statement.

Of course, that call was returning only the inner elements of those elements returned by the XPathSelectElements statement.

Matt.

Matt W