Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.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# HtmlAlityPack无法获取字符串索引器_C#_Html Parsing_Html Agility Pack - Fatal编程技术网

C# HtmlAlityPack无法获取字符串索引器

C# HtmlAlityPack无法获取字符串索引器,c#,html-parsing,html-agility-pack,C#,Html Parsing,Html Agility Pack,我想用HTML敏捷包解析HTML 当我用int搜索索引时,我得到的是结果 HtmlWeb htmlWeb = new HtmlWeb(); HtmlDocument htmlDocument = htmlWeb.Load("http://www.timeanddate.com/worldclock/georgia/tbilisi"); var s1 = htmlDocument.DocumentNode.Descendants().Where(x => x.HasAttributes &

我想用HTML敏捷包解析HTML

当我用int搜索索引时,我得到的是结果

HtmlWeb htmlWeb = new HtmlWeb();
HtmlDocument htmlDocument = htmlWeb.Load("http://www.timeanddate.com/worldclock/georgia/tbilisi");

var s1 = htmlDocument.DocumentNode.Descendants().Where(x => x.HasAttributes && x.Attributes[0].Value == "ct");
但是当我想用字符串索引器搜索atribute时,我得到了一个exeption

var s2 = htmlDocument.DocumentNode.Descendants().Where(a => a.HasAttributes && a.Attributes["id"].Value == "ct");
当我不使用LINQ和谓词委托时,一切都可以

Predicate<HtmlNode> pred = new Predicate<HtmlNode>(forpred);
List<HtmlNode> ss = htmlDocument.DocumentNode.Descendants().ToList().FindAll(pred);
public static bool forpred(HtmlNode node)
{
   if (node.HasAttributes)
   {    
      foreach (HtmlAttribute atribute in node.Attributes)
      {
         if (atribute.Name == "id" && atribute.Value == "ct")
         {
             return true;
         }
      }
   }
   return false;
}


//s1.ToList()[0].InnerHtml
//s2.ToList()[0].InnerHtml 
//ss[0].InnerHtml 
谓词pred=新谓词(forpred);
List ss=htmlDocument.DocumentNode.subjects().ToList().FindAll(pred);
公共静态bool forpred(HtmlNode节点)
{
if(node.HasAttributes)
{    
foreach(node.Attributes中的HtmlAttribute atribute)
{
如果(atribute.Name==“id”&&atribute.Value==“ct”)
{
返回true;
}
}
}
返回false;
}
//s1.ToList()[0]。InnerHtml
//s2.ToList()[0]。InnerHtml
//ss[0]。InnerHtml

因为有些跨距有属性,但没有id。您的代码可以如下所示:

var s2 = htmlDocument.DocumentNode
         .Descendants()
         .Where(a => a.Attributes["id"]!=null && a.Attributes["id"].Value == "ct")
         .ToList();