Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.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# 子体::*有效,但子体::th给出错误_C#_Html Agility Pack - Fatal编程技术网

C# 子体::*有效,但子体::th给出错误

C# 子体::*有效,但子体::th给出错误,c#,html-agility-pack,C#,Html Agility Pack,我一直在尝试制作一些使用HtmlAgilityPack的简单程序。以下程序检测“地址”或“电话”是否作为“th”标记的内容存在 我正在Visual Studio 12中运行此程序 如下所示的程序给出了一个错误 在>StructureClassifier.exe中发生类型为“System.NullReferenceException”的未处理异常 其他信息:对象引用未设置为对象的实例 请注意线路 var cells = table.SelectNodes("descenda

我一直在尝试制作一些使用HtmlAgilityPack的简单程序。以下程序检测“地址”或“电话”是否作为“th”标记的内容存在

我正在Visual Studio 12中运行此程序

如下所示的程序给出了一个错误

在>StructureClassifier.exe中发生类型为“System.NullReferenceException”的未处理异常

其他信息:对象引用未设置为对象的实例

请注意线路

            var cells = table.SelectNodes("descendant::th"); 
如果我用

            var cells = table.SelectNodes("descendant::*");
程序运行时没有任何错误

为什么下面的代码不起作用

using HtmlAgilityPack;
class Program
{
    static void Main(string[] args)
    {
        HtmlWeb w = new HtmlWeb();
        HtmlDocument doc = w.Load("http://www.prouds.com.au/stores?state=WA")

        foreach (HtmlNode table in doc.DocumentNode.SelectNodes("//table"))
        {
            var cells = table.SelectNodes("descendant::th"); // This returns error
            //It would work if I were to replace descendant::th to descendant::*
            Console.Write(cells.Count);
            Console.Write("\n");
            foreach (HtmlNode cell in cells)
            {
                string text = cell.InnerText;
                text = text.Trim();
                var equal = text.Equals("Address", StringComparison.OrdinalIgnoreCase);
                if (equal)
                {
                    Console.Write("Found Match\n");
                    Console.Write(text);
                    Console.Write("\n");
                }
                equal = text.Equals("Phone", StringComparison.OrdinalIgnoreCase);
                if (equal)
                {
                    Console.Write("Found Match\n");
                    Console.Write(text);
                    Console.Write("\n");
                }
            }
        }
    }
}

谢谢。

这可能是HtmlAgilityPack中的错误吗?如果这是一个错误,它与您提供的链接中的错误不同。我尝试使用1.4.6.0,并按照投票结果最高的答案从流中手动加载文档。还是会出错。