将XPathNavigator转换为HtmlAgilityPack的HtmlNode

时间:2012-02-06 15:26:58

标签: c# xpath .net-2.0 html-agility-pack xpathnavigator

可以将XPathNavigator转换为HtmlNode吗? 这是代码:

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?

}

1 个答案:

答案 0 :(得分:1)

示例中的

'it.Current'返回HtmlNodeNavigator的实例,该实例具有CurrentNode属性,该属性又返回HtmlNode

例如

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