Search code examples
c#xpath.net-2.0html-agility-packxpathnavigator

Convert XPathNavigator to HtmlAgilityPack's HtmlNode


It is possible to convert XPathNavigator to HtmlNode? Here is the code:

public string ContentByName(string name)
{
    if (name == null)
        throw new ArgumentNullException("name");

    XPathExpression expr = _CreateXPathExpression(String.Format("//meta[@name[Extensions:CaseInsensitiveComparison('{0}')]]", name));
    XPathNodeIterator it = _headNav.Select(expr);
    if (!it.MoveNext())
        return null;

    XPathNavigator node = it.Current;

    // How should I transform XPathNavigator node to HtmlNode here?

}

Solution

  • 'it.Current' in your example returns an instance of HtmlNodeNavigator which has a CurrentNode property which in turn returns the HtmlNode.

    For example

    HtmlNodeNavigator nodeNavigator = it.Current as HtmlNodeNavigator;
    HtmlNode node = nodeNavigator.CurrentNode;