How to: Find Sibling Nodes (XPath-LINQ to XML)
You might want to find all siblings of a node that have a specific name. The resulting collection might include the context node if the context node also has the specific name.
The XPath expression is:
../Book
Example
This example first finds a Book element, and then finds all sibling elements named Book. The resulting collection includes the context node.
This example uses the following XML document: Sample XML File: Books (LINQ to XML).
XDocument books = XDocument.Load("Books.xml");
XElement book =
books
.Root
.Elements("Book")
.Skip(1)
.First();
// LINQ to XML query
IEnumerable<XElement> list1 =
book
.Parent
.Elements("Book");
// XPath expression
IEnumerable<XElement> list2 = book.XPathSelectElements("../Book");
if (list1.Count() == list2.Count() &&
list1.Intersect(list2).Count() == list1.Count())
Console.WriteLine("Results are identical");
else
Console.WriteLine("Results differ");
foreach (XElement el in list1)
Console.WriteLine(el);
Dim books As XDocument = XDocument.Load("Books.xml")
Dim book As XElement = books.Root.<Book>.Skip(1).First()
' LINQ to XML query
Dim list1 As IEnumerable(Of XElement) = book.Parent.<Book>
' XPath expression
Dim list2 As IEnumerable(Of XElement) = book.XPathSelectElements("../Book")
If list1.Count() = list2.Count() And _
list1.Intersect(list2).Count() = list1.Count() Then
Console.WriteLine("Results are identical")
Else
Console.WriteLine("Results differ")
End If
For Each el As XElement In list1
Console.WriteLine(el)
Next
This example produces the following output:
Results are identical
<Book id="bk101">
<Author>Garghentini, Davide</Author>
<Title>XML Developer's Guide</Title>
<Genre>Computer</Genre>
<Price>44.95</Price>
<PublishDate>2000-10-01</PublishDate>
<Description>An in-depth look at creating applications
with XML.</Description>
</Book>
<Book id="bk102">
<Author>Garcia, Debra</Author>
<Title>Midnight Rain</Title>
<Genre>Fantasy</Genre>
<Price>5.95</Price>
<PublishDate>2000-12-16</PublishDate>
<Description>A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.</Description>
</Book>