I am trying to figure out how to use the HtmlAgilityPack.dll
library, of which I have version 1.11.59
. Till now I have been using it indirectly, through the PSParseHTML.
Since its not a Microsoft product, I cant just pull up its ms web page for one of its methods.
Relying on PowerShell, if I start typing $html.DocumentNode.GetAttribute
, PowerShell suggest method signatures:
string GetAttributeValue(string name, string def)
int GetAttributeValue(string name, int def)
bool GetAttributeValue(string name, bool def)
T GetAttributeValue[T](string name, T def)
I have tried to find online documentation for these methods to learn more about them and I have not found any documentation for this method. The official documentation for htmlAqilityPack does not list the above method.
So am wondering what is the source of it? This is my beyond my usual area, so I could overlooking something.
I am on PowerShell v7.4
Building on Mathias' helpful comment:
In PowerShell, "invoking" a method by omitting (...)
performs reflection and reports the method's signatures (parameter names, their types, and the return type), one for each method overload.
A simple example: running 'foo'.ToUpper
prints the signatures for all overloads of the string type's .ToUpper()
method:[1]
# OverloadDefinitions
-------------------
string ToUpper()
string ToUpper(cultureinfo culture)
The reported method signatures are guaranteed to exist (though there are some that can not be called from PowerShell).[2]
What isn't guaranteed to exist is documentation for them, and that indeed seems to be the case here:
As you've observed, the official documentation re attributes does not mention this method.
Even the list of code samples only covers the indexed .Attributes
property, in this sample.
However, the documentation's home page offers an AI-powered query facility that does produce results - via an intermediate page:
The result is oddly chatty, self-congratulatory - and partially incorrect; but it at least gives you the general idea.
The nice thing about the online samples being dotnetfiddle.net-based is that you can easily fork existing samples for your own experiments (you'll need an account, but it's free):
string GetAttributeValue(string name, string def)
overload; that is, it looks for an attribute with the specified name
and returns its value, if present; if absent, the def
value is returned.As Mathias notes, in the absence of documentation you can also refer to the source code, if available (and assuming that it matches the version of the code you're running):
Fortunately, it is.
Unfortunately, even though the source-code comments there are structured in a way that is suitable for auto-generating documentation from them, this hasn't been done as of this writing.
[1] Note an inconsistency in how PowerShell reports these signatures: While referring to types in PowerShell normally requires enclosing their names in [...]
, e.g. [string]
, the method-signature notation uses C#-style type-name-only references, e.g. string
- except in the case of generic type arguments (e.g. ... GetAttributeValue[T](...)
, where [T]
contrasts with .NET's <T>
notation.)
[2] For instance, in Windows PowerShell (the legacy, ships-with-Windows, Windows-only edition of PowerShell whose latest and last version is 5.1) you cannot call methods that require generic type parameters (except if there's only one and only if that type can be inferred from the call's arguments). PowerShell (Core) 7 now supports such calls, but neither edition supports use of ByRef-like types (ref struct
in C#), such as System.Span<T>