If you need a more comprehensive solution, you can write an extension function for the XPath processor that will perform case-insensitive comparisons. This is quite a bit of code, but you only write it once.
After implementing the extension, you can write your request as follows
"//meta[@name[Extensions:CaseInsensitiveComparison('Keywords')]]"
Where Extensions:CaseInsensitiveComparison
is the extension function implemented in the example below.
NOTE: this is not very well tested. I just threw it together for this answer, so error handling, etc. does not exist!
Below is the XSLT custom context code that provides one or more extension functions
using System; using System.Xml.XPath; using System.Xml.Xsl; using System.Xml; using HtmlAgilityPack; public class XsltCustomContext : XsltContext { public const string NamespaceUri = "http://XsltCustomContext"; public XsltCustomContext() { } public XsltCustomContext(NameTable nt) : base(nt) { } public override IXsltContextFunction ResolveFunction(string prefix, string name, XPathResultType[] ArgTypes) {
Then you can use the above extension function in your XPath queries, here is an example for our case
class Program { static string html = "<html><meta name=\"keywords\" content=\"HTML, CSS, XML\" /></html>"; static void Main(string[] args) { HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(html); XPathNavigator nav = doc.CreateNavigator();
source share