Skip to content

Commit 2d9b4e3

Browse files
jameshkramermairaw
andauthored
30). Add topic files 87 to 90 (#18259)
* 30). Add topic files 87 to 90 * Apply suggestions from code review Co-authored-by: Maira Wenzel <[email protected]> Co-authored-by: Maira Wenzel <[email protected]>
1 parent 96ebfb2 commit 2d9b4e3

13 files changed

+388
-447
lines changed

.openpublishing.redirection.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1721,6 +1721,26 @@
17211721
"redirect_url": "/dotnet/standard/linq/find-preceding-siblings",
17221722
"redirect_document_id": true
17231723
},
1724+
{
1725+
"source_path": "docs/csharp/programming-guide/concepts/linq/how-to-find-an-attribute-of-the-parent-xpath-linq-to-xml.md",
1726+
"redirect_url": "/dotnet/standard/linq/find-attribute-parent",
1727+
"redirect_document_id": true
1728+
},
1729+
{
1730+
"source_path": "docs/csharp/programming-guide/concepts/linq/how-to-find-attributes-of-siblings-with-a-specific-name-xpath-linq-to-xml.md",
1731+
"redirect_url": "/dotnet/standard/linq/find-attributes-siblings-specific-name",
1732+
"redirect_document_id": true
1733+
},
1734+
{
1735+
"source_path": "docs/csharp/programming-guide/concepts/linq/how-to-find-elements-with-a-specific-attribute-xpath-linq-to-xml.md",
1736+
"redirect_url": "/dotnet/standard/linq/find-elements-specific-attribute",
1737+
"redirect_document_id": true
1738+
},
1739+
{
1740+
"source_path": "docs/csharp/programming-guide/concepts/linq/how-to-find-sibling-nodes-xpath-linq-to-xml.md",
1741+
"redirect_url": "/dotnet/standard/linq/find-sibling-nodes",
1742+
"redirect_document_id": true
1743+
},
17241744
{
17251745
"source_path": "docs/csharp/programming-guide/concepts/threading/how-to-use-a-thread-pool.md",
17261746
"redirect_url": "/dotnet/api/system.threading.threadpool.queueuserworkitem"
@@ -5203,6 +5223,26 @@
52035223
"redirect_url": "/dotnet/standard/linq/find-preceding-siblings",
52045224
"redirect_document_id": false
52055225
},
5226+
{
5227+
"source_path": "docs/visual-basic/programming-guide/concepts/linq/how-to-find-an-attribute-of-the-parent-xpath-linq-to-xml.md",
5228+
"redirect_url": "/dotnet/standard/linq/find-attribute-parent",
5229+
"redirect_document_id": false
5230+
},
5231+
{
5232+
"source_path": "docs/visual-basic/programming-guide/concepts/linq/how-to-find-attributes-of-siblings-with-a-specific-name.md",
5233+
"redirect_url": "/dotnet/standard/linq/find-attributes-siblings-specific-name",
5234+
"redirect_document_id": false
5235+
},
5236+
{
5237+
"source_path": "docs/visual-basic/programming-guide/concepts/linq/how-to-find-elements-with-a-specific-attribute.md",
5238+
"redirect_url": "/dotnet/standard/linq/find-elements-specific-attribute",
5239+
"redirect_document_id": false
5240+
},
5241+
{
5242+
"source_path": "docs/visual-basic/programming-guide/concepts/linq/how-to-find-sibling-nodes-xpath-linq-to-xml.md",
5243+
"redirect_url": "/dotnet/standard/linq/find-sibling-nodes",
5244+
"redirect_document_id": false
5245+
},
52065246
{
52075247
"source_path": "docs/visual-basic/programming-guide/concepts/threading/how-to-use-a-thread-pool.md",
52085248
"redirect_url": "/dotnet/api/system.threading.threadpool.queueuserworkitem"

docs/csharp/programming-guide/concepts/linq/how-to-find-an-attribute-of-the-parent-xpath-linq-to-xml.md

Lines changed: 0 additions & 50 deletions
This file was deleted.

docs/csharp/programming-guide/concepts/linq/how-to-find-attributes-of-siblings-with-a-specific-name-xpath-linq-to-xml.md

Lines changed: 0 additions & 51 deletions
This file was deleted.

docs/csharp/programming-guide/concepts/linq/how-to-find-elements-with-a-specific-attribute-xpath-linq-to-xml.md

Lines changed: 0 additions & 52 deletions
This file was deleted.

docs/csharp/programming-guide/concepts/linq/how-to-find-sibling-nodes-xpath-linq-to-xml.md

Lines changed: 0 additions & 69 deletions
This file was deleted.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
---
2+
title: How to find an attribute of the parent - LINQ to XML
3+
description: "Learn how to navigate to an element and find an attribute of its parent. Two methods are shown: one uses XPathEvaluate, the other uses LINQ to XML query."
4+
ms.date: 07/20/2015
5+
dev_langs:
6+
- "csharp"
7+
- "vb"
8+
ms.assetid: dbef9d89-a5c4-431f-80cc-7a2ebf323f86
9+
---
10+
11+
# How to find an attribute of the parent (LINQ to XML)
12+
13+
This article shows how to use <xref:System.Xml.XPath.Extensions.XPathEvaluate%2A> to find an attribute of a parent element, and how to use LINQ to XML query to do the same thing.
14+
15+
## Example: Find the `Author` element, and then find the `id` attribute of its parent
16+
17+
This example first finds an `Author` element in XML document [Sample XML file: Books](sample-xml-file-books.md), and then finds the `id` attribute of its parent element.
18+
19+
The XPath expression is `../@id`.
20+
21+
```csharp
22+
XDocument books = XDocument.Load("Books.xml");
23+
24+
XElement author =
25+
books
26+
.Root
27+
.Element("Book")
28+
.Element("Author");
29+
30+
// LINQ to XML query
31+
XAttribute att1 =
32+
author
33+
.Parent
34+
.Attribute("id");
35+
36+
// XPath expression
37+
XAttribute att2 = ((IEnumerable)author.XPathEvaluate("../@id")).Cast<XAttribute>().First();
38+
39+
if (att1 == att2)
40+
Console.WriteLine("Results are identical");
41+
else
42+
Console.WriteLine("Results differ");
43+
Console.WriteLine(att1);
44+
```
45+
46+
```vb
47+
Dim books As XDocument = XDocument.Load("Books.xml")
48+
Dim author As XElement = books.Root.<Book>.<Author>.FirstOrDefault()
49+
50+
' LINQ to XML query
51+
Dim att1 As XAttribute = author.Parent.Attribute("id")
52+
53+
' XPath expression
54+
Dim att2 As XAttribute = DirectCast(author.XPathEvaluate("../@id"), _
55+
IEnumerable).Cast(Of XAttribute)().First()
56+
57+
If att1 Is att2 Then
58+
Console.WriteLine("Results are identical")
59+
Else
60+
Console.WriteLine("Results differ")
61+
End If
62+
Console.WriteLine(att1)
63+
```
64+
65+
This example produces the following output:
66+
67+
```output
68+
Results are identical
69+
id="bk101"
70+
```
71+
72+
## See also
73+
74+
- [LINQ to XML for XPath Users (Visual Basic)](../../visual-basic/programming-guide/concepts/linq/linq-to-xml-for-xpath-users.md)

0 commit comments

Comments
 (0)