Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
This article shows how to use XPathEvaluate to find the sibling that immediately precedes a node, and how to use LINQ to XML query to do the same thing. Due to the difference in the semantics of positional predicates for the preceding sibling axes in XPath as opposed to LINQ to XML, this is one of the more interesting comparisons.
Example: Find the next to last element
In this example, the LINQ to XML query uses the Last operator to find the last node in the collection returned by ElementsBeforeSelf. By contrast, the XPath expression uses a predicate with a value of 1 to find the immediately preceding element.
XElement root = XElement.Parse(
@"<Root>
<Child1/>
<Child2/>
<Child3/>
<Child4/>
</Root>");
XElement child4 = root.Element("Child4");
// LINQ to XML query
XElement el1 =
child4
.ElementsBeforeSelf()
.Last();
// XPath expression
XElement el2 =
((IEnumerable)child4
.XPathEvaluate("preceding-sibling::*[1]"))
.Cast<XElement>()
.First();
if (el1 == el2)
Console.WriteLine("Results are identical");
else
Console.WriteLine("Results differ");
Console.WriteLine(el1);
Dim root As XElement = _
<Root>
<Child1/>
<Child2/>
<Child3/>
<Child4/>
</Root>
Dim child4 As XElement = root.Element("Child4")
' LINQ to XML query
Dim el1 As XElement = child4.ElementsBeforeSelf().Last()
' XPath expression
Dim el2 As XElement = _
DirectCast(child4.XPathEvaluate("preceding-sibling::*[1]"), _
IEnumerable).Cast(Of XElement)().First()
If el1 Is el2 Then
Console.WriteLine("Results are identical")
Else
Console.WriteLine("Results differ")
End If
Console.WriteLine(el1)
This example produces the following output:
Results are identical
<Child3 />