この記事では、XPathSelectElements を使用し、特定の名前を持つノードの兄弟をすべて検索する方法と、LINQ to XML クエリを使用して同じことをする方法を紹介します。
注意
コンテキスト ノードにも特定の名前が含まれる場合、結果的に生成されるコレクションにはコンテキスト ノードも含まれます。
例: Book
という名前の要素と Book
という名前のすべての兄弟要素を検索する
この例ではまず、XML ドキュメント「サンプル XML ファイル : 書籍」に含まれる Book
要素を検索し、それから Book
という名前の兄弟要素をすべて検索します。 結果のコレクションにはコンテキスト ノードが含まれます。
XPath 式は ../Book
です。
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
この例を実行すると、次の出力が生成されます。
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>
関連項目
GitHub で Microsoft と共同作業する
このコンテンツのソースは GitHub にあります。そこで、issue や pull request を作成および確認することもできます。 詳細については、共同作成者ガイドを参照してください。
.NET