Reputation: 53
I have XElement x
<projects><project><id>2</id><name>Project A</name></project>
<project><id>7</id><name>Blue-Leafed Project B</name></project></projects>
Than I am using the XPathSelectElements and expecting to get 2 nodes:
var projects = x.XPathSelectElements("/projects/project");
But result = null;
I was also trying to slightly change XPath
result = null;
What is wrong with this?
Upvotes: 1
Views: 1834
Reputation: 134801
What is most likely happened is that you loaded your document as an XElement
and therefore x
is already referring to the root node projects
. Your queries have to be relative to that node and that node clearly doesn't have a projects
child. You're trying to select the child project
elements relative to your projects
node so your query should be:
var projects = x.XPathSelectElements("project");
Though in this case, you don't really need to use xpath, just use the Elements()
method instead.
var projects = x.Elements("project");
You generally should use XDocument
objects to load the document instead of XElement
, otherwise you'd run into these kinds of problems.
Upvotes: 1
Reputation: 7341
You can try this one:
var projects = x.XPathSelectElements("./projects/project");
Upvotes: 0