Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/8.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# GetEnumerator返回null_C# - Fatal编程技术网

C# GetEnumerator返回null

C# GetEnumerator返回null,c#,C#,对象htmlDocument.Body.All不为空。为什么GetEnumerator()返回null IEnumerator<HtmlElement> hm = htmlDocument.Body.All.GetEnumerator() as IEnumerator<HtmlElement>; IEnumerator hm=htmlDocument.Body.All.GetEnumerator()作为 电子计算器; 您正在使用as强制转换它,如果as失败

对象
htmlDocument.Body.All
不为空。为什么
GetEnumerator()
返回null

IEnumerator<HtmlElement> hm =  htmlDocument.Body.All.GetEnumerator() as      
IEnumerator<HtmlElement>;
IEnumerator hm=htmlDocument.Body.All.GetEnumerator()作为
电子计算器;

您正在使用
as
强制转换它,如果
as
失败,它将返回
null
。像这样尝试一下,看看确切的返回类型是什么,而不需要假设任何内容或强制转换:

var hm =  htmlDocument.Body.All.GetEnumerator();

我想你想要这个。虽然你为什么需要一个
枚举器
我不知道

IEnumerator<HtmlElement> hm =  htmlDocument.Body.All
               .OfType<HtmlElement>()
               .GetEnumerator();
IEnumerator hm=htmlDocument.Body.All
第()类
.GetEnumerator();
在大多数情况下,你会发现,你可以做的事情更容易与

foreach(var element in htmlDocument.Body.All.OfType<HtmlElement>())
{
     //Stuff
}
foreach(htmlDocument.Body.All.OfType()中的var元素)
{
//东西
}

我可以看到.NET framework中有3个
HtmlElement
类,这是代码片段中唯一标识的特定类型。您是否愿意添加更多的信息,以便我们知道您在这里使用的是哪些类?因为我想将html解析到我的模型中。