@@ -16,11 +16,12 @@ public static class Extensions
16
16
/// Creates a new navigator for the given document.
17
17
/// </summary>
18
18
/// <param name="document">The document to extend.</param>
19
+ /// <param name="ignoreNamespaces"></param>
19
20
/// <returns>The navigator for XPath expressions.</returns>
20
- public static XPathNavigator CreateNavigator ( this IDocument document )
21
+ public static XPathNavigator CreateNavigator ( this IDocument document , bool ignoreNamespaces = true )
21
22
{
22
23
var doc = document ?? throw new ArgumentNullException ( nameof ( document ) ) ;
23
- return new HtmlDocumentNavigator ( doc , doc . DocumentElement ) ;
24
+ return new HtmlDocumentNavigator ( doc , doc . DocumentElement , ignoreNamespaces ) ;
24
25
}
25
26
26
27
[ DebuggerStepThrough ]
@@ -35,14 +36,29 @@ internal static string GetOrAdd(this XmlNameTable table, string array)
35
36
/// </summary>
36
37
/// <param name="element">The element to start looking from.</param>
37
38
/// <param name="xpath">The XPath expression.</param>
39
+ /// <param name="ignoreNamespaces"></param>
38
40
/// <returns>The node matching <paramref name="xpath"/> query, if any.</returns>
39
41
/// <exception cref="ArgumentNullException">Throws if <paramref name="element"/> or <paramref name="xpath"/> is <c>null</c></exception>
40
- public static INode SelectSingleNode ( this IElement element , string xpath )
42
+ public static INode SelectSingleNode ( this IElement element , string xpath , bool ignoreNamespaces = true )
43
+ {
44
+ return element . SelectSingleNode ( xpath , new XmlNamespaceManager ( new NameTable ( ) ) , ignoreNamespaces ) ;
45
+ }
46
+
47
+ /// <summary>
48
+ /// Selects a single node (or returns null) matching the <see cref="XPath"/> expression.
49
+ /// </summary>
50
+ /// <param name="element">The element to start looking from.</param>
51
+ /// <param name="xpath">The XPath expression.</param>
52
+ /// <param name="resolver"></param>
53
+ /// <param name="ignoreNamespaces"></param>
54
+ /// <returns>The node matching <paramref name="xpath"/> query, if any.</returns>
55
+ /// <exception cref="ArgumentNullException">Throws if <paramref name="element"/> or <paramref name="xpath"/> is <c>null</c></exception>
56
+ public static INode SelectSingleNode ( this IElement element , string xpath , IXmlNamespaceResolver resolver , bool ignoreNamespaces = true )
41
57
{
42
58
var el = element ?? throw new ArgumentNullException ( nameof ( element ) ) ;
43
59
var xp = xpath ?? throw new ArgumentNullException ( nameof ( xpath ) ) ;
44
- var nav = new HtmlDocumentNavigator ( el . Owner , el ) ;
45
- var it = nav . SelectSingleNode ( xp ) ;
60
+ var nav = new HtmlDocumentNavigator ( el . Owner , el , ignoreNamespaces ) ;
61
+ var it = nav . SelectSingleNode ( xp , resolver ?? new XmlNamespaceManager ( new NameTable ( ) ) ) ;
46
62
return ( ( HtmlDocumentNavigator ) it ) ? . CurrentNode ;
47
63
}
48
64
@@ -51,14 +67,29 @@ public static INode SelectSingleNode(this IElement element, string xpath)
51
67
/// </summary>
52
68
/// <param name="element">The element to start looking from.</param>
53
69
/// <param name="xpath">The XPath expression.</param>
70
+ /// <param name="ignoreNamespaces"></param>
71
+ /// <returns>List of nodes matching <paramref name="xpath"/> query.</returns>
72
+ /// <exception cref="ArgumentNullException">Throws if <paramref name="element"/> or <paramref name="xpath"/> is <c>null</c></exception>
73
+ public static List < INode > SelectNodes ( this IElement element , string xpath , bool ignoreNamespaces = true )
74
+ {
75
+ return element . SelectNodes ( xpath , new XmlNamespaceManager ( new NameTable ( ) ) , ignoreNamespaces ) ;
76
+ }
77
+
78
+ /// <summary>
79
+ /// Selects a list of nodes matching the <see cref="XPath"/> expression.
80
+ /// </summary>
81
+ /// <param name="element">The element to start looking from.</param>
82
+ /// <param name="xpath">The XPath expression.</param>
83
+ /// <param name="resolver"></param>
84
+ /// <param name="ignoreNamespaces"></param>
54
85
/// <returns>List of nodes matching <paramref name="xpath"/> query.</returns>
55
86
/// <exception cref="ArgumentNullException">Throws if <paramref name="element"/> or <paramref name="xpath"/> is <c>null</c></exception>
56
- public static List < INode > SelectNodes ( this IElement element , string xpath )
87
+ public static List < INode > SelectNodes ( this IElement element , string xpath , IXmlNamespaceResolver resolver , bool ignoreNamespaces = true )
57
88
{
58
89
var el = element ?? throw new ArgumentNullException ( nameof ( element ) ) ;
59
90
var xp = xpath ?? throw new ArgumentNullException ( nameof ( xpath ) ) ;
60
- var nav = new HtmlDocumentNavigator ( el . Owner , el ) ;
61
- var it = nav . Select ( xp ) ;
91
+ var nav = new HtmlDocumentNavigator ( el . Owner , el , ignoreNamespaces ) ;
92
+ var it = nav . Select ( xp , resolver ?? new XmlNamespaceManager ( new NameTable ( ) ) ) ;
62
93
var result = new List < INode > ( ) ;
63
94
64
95
while ( it . MoveNext ( ) )
0 commit comments