Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.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# Foreach迭代空_C#_Linq_Sharepoint_Linq To Xml - Fatal编程技术网

C# Foreach迭代空

C# Foreach迭代空,c#,linq,sharepoint,linq-to-xml,C#,Linq,Sharepoint,Linq To Xml,我使用LINQ过滤从Sharepoint WSS3的listService.GetListItems方法返回的XmlNode 这是它返回的XML: 我注意到最后一行与其他行不同,它不包含以下属性 ows_ContentType ows_LinkFilenameNoMenu ows_LinkFilename ows_BaseName ows__EditMenuTableStart 因此,记住这一点,我使用LINQ过滤结果: XmlNode items = listService.GetLis

我使用LINQ过滤从Sharepoint WSS3的listService.GetListItems方法返回的XmlNode

这是它返回的XML:

我注意到最后一行与其他行不同,它不包含以下属性

ows_ContentType
ows_LinkFilenameNoMenu
ows_LinkFilename
ows_BaseName 
ows__EditMenuTableStart
因此,记住这一点,我使用LINQ过滤结果:

XmlNode items = listService.GetListItems(listName, string.Empty, query, viewFields, string.Empty, queryOptions, g.ToString());

// Namespace for z: prefix
XNamespace xns = "#RowsetSchema";

XElement root;
using (XmlReader xr = new XmlNodeReader(items)) { root = XElement.Load(xr); }

// This query returns XElements that are Folder types
IEnumerable<XElement> result = from child in root.Descendants(xns + "row")
                               where child.Attribute("ows_ContentType").Value != null && child.Attribute("ows_ContentType").Value == "Folder"
                               select child;

foreach (XElement xml in result)
{
    //Exception when attempts to loop final XElement
}
但是,当我遍历结果时,会得到一个NullReferenceException。在Foreach循环中,它将愉快地迭代每个对象,直到到达最后一个对象,然后抛出异常

最后一个对象与其他行不同,我通过消除过程了解到这一点,我看到其他每一行都循环通过,应该从结果中过滤掉,因为它没有ows_ContentType属性

我将LINQ更改为where child.Attributeows\u ContentType.Value!=null&&child.Attributeows\u ContentType.Value==文件夹,尝试筛选任何可能包含null值但结果相同的内容。 我已经看了一些,我似乎有正确的语法

有人能解释一下是什么导致最后一个XElement返回null吗?
我真的不明白为什么它会在中添加一个null实例。您需要检查元素是否为null,而不是元素的值

child.Attribute("ows_ContentType") != null && child.Attribute("ows_ContentType").Value == "Folder"

最后一个节点不包含ows_ContentType属性,但where子句正在查找缺少的属性的值

可能你需要

where child.Attribute("ows_ContentType") != null 
&& child.Attribute("ows_ContentType").Value == "Folder"

调用不存在属性的值将导致nullreference,因为节点根本不存在

child.Attribute("ows_ContentType").Value
通过调用缺少元素的值引发异常

改用这个:

child.Attribute("ows_ContentType") != null
实施:

IEnumerable<XElement> result = from child in root.Descendants(xns + "row")
                           where child.Attribute("ows_ContentType") != null && child.Attribute("ows_ContentType").Value == "Folder"
                           select child;

请注意,LINQ2XML只允许您使用stringsomeXElement和stringsomeXAttribute,如果XElement或XAttribute为null,则会为null;如果XElement或XAttribute存在,则会为null。这意味着您的代码可以缩短以供使用

IEnumerable<XElement> result = from child in root.Descendants(xns + "row")
                               where (string)child.Attribute("ows_ContentType") == "Folder"
                               select child;

您是否正在修改循环中的所有节点?child.Attributeows\u ContentType!=null而不是调用值?从不存在的节点调用值将抛出一个exceptionAh,这非常有意义。谢谢你!真的要确保child.Attributeows_ContentType不为null:为了我自己的利益,根据这个答案,如果WARE子句发现一个结果为false的条件,它就会终止吗?否则,当它检查节点是否为空时,它也会请求值并检查是否为空,因为节点为空而导致执行选项?@Rawlin:D'oh,这是怎么回事?@middelpat:抱歉,但我不理解这个问题。如果caluse未终止,则当条件为false时,它将跳过项。如果child.Attributeows_ContentType==null,它将不计算&&条件的第二部分。请看这里的短路@BinaryWorrier您的评论刚刚回答了我的问题。谢谢: