Reputation: 8926
I came across an unusual problem. I have this XML:
<T durationMs="400">
<foo durationMs="407">
<foo-child durationMs="307" />
</foo>
<bar durationMs="208">
<bar-child durationMs="108" />
</bar>
</T>
I am using XPathExtentions to get an XElement out of this XML:
var xe = XElement.Parse(s);
var foo = xe.XPathSelectElement("/T/foo")
It returns nothing. However if I use:
var foo = xe.XPathSelectElement("./foo")
It gets an elements. So what's the difference between dot and slash in this case?
Upvotes: 1
Views: 3537
Reputation: 32797
/
selects from the root
node.
So with /T/foo
it's trying to match T->T->foo
which for sure won't match
.
depicts the current node in which case it would be the root node
/foo
would work
Upvotes: 4
Reputation: 20575
.
selects the current node.
/
Selects from the root node.
//
Selects nodes in the document from the current node that match the selection no matter where they are.
XPath Syntax gives you a brief idea of how selections are done
In your case ./foo
denotes selection from root node i.e. T
Upvotes: 2