Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用HtmlAgilityPack通过innerHTML进行解析_C#_Xpath_Html Agility Pack_Selectsinglenode - Fatal编程技术网

C# 使用HtmlAgilityPack通过innerHTML进行解析

C# 使用HtmlAgilityPack通过innerHTML进行解析,c#,xpath,html-agility-pack,selectsinglenode,C#,Xpath,Html Agility Pack,Selectsinglenode,只是想弄清楚如何从已经解析的信息中解析信息 foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//div [@class=\"result-link\"]")) { if (node == null) Console.WriteLine("debug"); else { //string h_url = node.Attributes["a"].Value; Co

只是想弄清楚如何从已经解析的信息中解析信息

foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//div [@class=\"result-link\"]"))
{
    if (node == null)
        Console.WriteLine("debug");
    else
    {
        //string h_url = node.Attributes["a"].Value;
        Console.WriteLine(node.InnerHtml);
    }
}
所以你可以看到我试图用“string h_url”声明做什么。在“result link”div类中有一个a href属性,我试图获取href值。所以基本上是联系

我好像想不出来。我已尝试使用属性数组:

string h_url = node.Attributes["//a[@href].Value;

没有运气

可以使用XPath选择与当前节点相关的元素:

HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes("//div[@class='result-link']");
if (nodes != null)
{
    foreach (HtmlNode node in nodes)
    {
        HtmlNode a = node.SelectSingleNode("a[@href]");
        if (a != null)
        {
            // use  a.Attributes["href"];
        }

        // etc...
    }
}