You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: support/developer/dotnet/framework/general/query-xpathdocument-xpath-csharp.md
+21-21Lines changed: 21 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,20 +1,20 @@
1
1
---
2
-
title: Query XPathDocument by using an XPath expression
3
-
description: Demonstrates how to query an XPathDocument object with an XML Path Language (XPath) expression using the XPathNavigator class.
2
+
title: Query XPathDocument By Using an XPath Expression
3
+
description: Demonstrates how to query an XPathDocument object by using an XML Path Language (XPath) expression that uses the XPathNavigator class.
4
4
ms.date: 07/07/2025
5
5
ms.reviewer: JAYAPST
6
6
ms.topic: how-to
7
7
ms.custom: sap:Class Library Namespaces
8
8
9
-
#customer intent: As a developer, I want to query XML documents using XPath expressions so that I can use XML documents with my application.
9
+
#customer intent: As a developer, I want to query XML documents by using XPath expressions so that I can use XML documents together with my application.
10
10
---
11
11
# Query XML with an XPath expression in Visual C\#
12
12
13
-
This article introduces how to query an `XPathDocument` object with an XML Path Language (XPath) expression by using the `XPathNavigator` class.
13
+
This article discusses how to query an `XPathDocument` object by using an XML Path Language (XPath) expression that uses the `XPathNavigator` class.
14
14
15
15
XPath is used programmatically to evaluate expressions and select specific nodes in a document.
16
16
17
-
This article refers to the Microsoft .NET Framework Class Library namespace `System.Xml.XPath`.
17
+
This article refers to the Microsoft .NET Framework Class Library namespace,`System.Xml.XPath`.
18
18
19
19
_Applies to:_ Visual Studio, .NET Framework
20
20
@@ -29,16 +29,16 @@ This article assumes that you're familiar with the following topics:
29
29
- Creating and reading an XML file
30
30
- XPath syntax
31
31
32
-
## Query XPathDocument with an XPath expression
32
+
## Query XPathDocument that has an XPath expression
33
33
34
-
1.Create a new Visual C# Console Application in Visual Studio.
34
+
1.In Microsoft Visual Studio, create a Visual C# Console Application.
35
35
36
36
> [!NOTE]
37
-
> This example uses a file named *Books.xml*. You can create your own Books.xml file, or you can use the sample that is included with the .NET Software Development Kit (SDK) quickstarts.
37
+
> This example uses a file that's named *Books.xml*. You can create your own Books.xml file, or you can use the sample that's included with the .NET Software Development Kit (SDK) quickstarts.
38
38
>
39
-
> If you don't have the quickstarts installed and don't want to install them, see the [Related content](#related-content) section for the Books.xml download location.
39
+
> If you don't have the quickstarts installed, and you don't want to install them, see the "[Related content](#related-content)" section for the Books.xml download location.
40
40
>
41
-
> If you have the quickstarts installed, the file can be found in `Program Files\Microsoft.NET\FrameworkSDK\Samples\Quickstart\Howto\Samples\Xml\Transforxml\VB` folder. You can copy the file to the `\Bin\Debug`folder, which is located under the folder in which you created this project.
41
+
> If you have the quickstarts installed, the Books.xml file can be found in the `Program Files\Microsoft.NET\FrameworkSDK\Samples\Quickstart\Howto\Samples\Xml\Transforxml\VB` folder. You can copy the file to the `\Bin\Debug`subfolder of the folder in which you created this project.
42
42
43
43
2. Make sure that the project references the `System.Xml` namespace.
44
44
3. Use the `using` statement on the `Xml` and `XPath` namespaces so that you aren't required to qualify declarations in those namespaces later in your code. You can use the `using` statement before any other declarations, as follows:
@@ -48,7 +48,7 @@ This article assumes that you're familiar with the following topics:
48
48
usingSystem.Xml.XPath;
49
49
```
50
50
51
-
4. Declare the appropriate variables. Declare an `XPathDocument` object to hold the XML document, an `XpathNavigator` object to evaluate XPath expressions, and an `XPathNodeIterator` object to iterate through selected nodes. Declare a `String` object to hold the XPath expressions. Add the declaration code in the `Main` function in `Class1`.
51
+
4. Declare the appropriate variables. Declare an `XPathDocument` object to hold the XML document, an `XpathNavigator` object to evaluate XPath expressions, and an `XPathNodeIterator` object to iterate through selected nodes. Declare a `String` object to hold the XPath expressions. Add the declaration code to the `Main` function in `Class1`.
52
52
53
53
```csharp
54
54
XPathNavigatornav;
@@ -57,21 +57,21 @@ This article assumes that you're familiar with the following topics:
57
57
StringstrExpression;
58
58
```
59
59
60
-
5. Load an `XPathDocument` with the sample file Books.xml. The `XPathDocument` class uses Extensible Stylesheet Language Transformations (XSLT) to provide a fast and performance-oriented cache for XML document processing. It's similar to the XML Document Object Model (DOM) but is highly optimized for XSLT processing and the `XPath` data model.
60
+
5. Load an `XPathDocument`together with the sample file, Books.xml. The `XPathDocument` class uses Extensible Stylesheet Language Transformations (XSLT) to provide a fast and performance-oriented cache for XML document processing. It's similar to the XML Document Object Model (DOM) but is highly optimized for XSLT processing and the `XPath` data model.
61
61
62
62
```csharp
63
63
// Open the XML.
64
64
docNav=newXPathDocument(@"c:\books.xml");
65
65
```
66
66
67
-
6. Create an `XPathNavigator` from the document. The `XPathNavigator` object is used for read-only XPath queries. The XPath queries may return a resulting value or many nodes.
67
+
6. Create an `XPathNavigator` from the document. The `XPathNavigator` object is used for read-only XPath queries. The XPath queries might return a resulting value or many nodes.
68
68
69
69
```csharp
70
70
// Create a navigator to query with XPath.
71
71
nav=docNav.CreateNavigator();
72
72
```
73
73
74
-
7. Create an XPath expression to find the average cost of a book. This XPath expression returns a single value. For full details on XPath syntax, see XPath Syntax in the [References](#related-content) section.
74
+
7. Create an XPath expression to find the average cost of a book. This XPath expression returns a single value. For full details about XPath syntax, see **XPath Syntax** in the "[References](#related-content)" section.
75
75
76
76
```csharp
77
77
// Find the average cost of a book.
@@ -86,21 +86,21 @@ This article assumes that you're familiar with the following topics:
86
86
Console.WriteLine("The average cost of the books are {0}", nav.Evaluate(strExpression));
87
87
```
88
88
89
-
9. Create an XPath expression to find all of the books that cost more than 10 dollars. This XPath expression returns only Title nodes from the XML source.
89
+
9. Create an XPath expression to find all the books that cost more than 10 dollars. This XPath expression returns only Title nodes from the XML source.
90
90
91
91
```csharp
92
92
// Find the title of the books that are greater then $10.00.
10. Create an `XPathNodeIterator` for the nodes that are selected with the `Select` method of the `XPathNavigator`. The `XPathNodeIterator` represents an XPath nodeset, and supports operations on this nodeset.
96
+
10. Create an `XPathNodeIterator` for the nodes that are selected together with the `Select` method of the `XPathNavigator`. The `XPathNodeIterator` represents an XPath nodeset, and supports operations on this nodeset.
97
97
98
98
```csharp
99
99
// Select the node and place the results in an iterator.
The exception error occurs on the following line of code:
135
+
This exception occurs on the following line of code:
136
136
137
137
```csharp
138
138
docNav=newXPathDocument("c:\\books.xml");
139
139
```
140
140
141
-
The exception error is caused by an invalid processing instruction. For example, the processing instruction may contain extraneous spaces. The following example is an invalid processing instruction:
141
+
The error is caused by an invalid processing instruction. For example, the processing instruction might contain extraneous spaces. The following example is an invalid processing instruction:
0 commit comments