简体   繁体   English

带有一项的C#Xml.Linq集合使用.ToList()返回“对象引用未设置为对象的实例”

[英]C# Xml.Linq collection with one item returns “Object reference not set to an instance of an object” using .ToList()

I have a List<XElement> with a number of XML elements. 我有一个带有许多XML元素的List<XElement> When I use the Where() method, I manage to find the one item successfully. 当我使用Where()方法时,我设法成功找到了一项。 Using First() returns that item successfully, and if I use Any() it returns true. 使用First()成功返回该项目,如果我使用Any()则返回true。 However if I use Count() or ToList() it returns Object reference not set to an instance of an object. 但是,如果我使用Count()ToList()它将返回Object reference not set to an instance of an object.

Many thanks in advance. 提前谢谢了。

    //Elements:
    <meta name="ncc:sidebars" content="0" xmlns="http://www.w3.org/1999/xhtml" />
    <meta name="ncc:setInfo" content="1 of 1" xmlns="http://www.w3.org/1999/xhtml" />
    <meta name="ncc:tocItems" content="12" xmlns="http://www.w3.org/1999/xhtml" />
    <meta name="ncc:totalTime" content="8:02:54" xmlns="http://www.w3.org/1999/xhtml" />
    <!-- another 30 other elements... -->
    <meta http-equiv="Content-type" content="text/html; charset=ISO-8859-1" xmlns="http://www.w3.org/1999/xhtml" />

public static List<XElement> GetElements(this List<XElement> els, String nameTag)
{
    var elementsFound = els.Where(e => e.Attribute("name").Value.ToLower() == "ncc:totaltime");
    if (elementsFound.Any())
        return elementsFound.ToList();
    else
        throw new Exception("Some text");
}

this should work: 这应该工作:

var elementsFound = els.Where(e => e.Attribute("name") != null && e.Attribute("name").Value.ToLower() == nameTag);
if (elementsFound.Any())
    return elementsFound.ToList();
else
    throw new Exception("Some text");

I think one or more of your XElement objects do not have a attribute named "name". 我认为您的一个或多个XElement对象没有名为“ name”的属性。 All your LINQ queries ("where" in this case) will be only executed if you actually use the result. 仅当您实际使用结果时,才会执行所有LINQ查询(在这种情况下为“ where”)。

There exists an item in your list which has an attribute which returns null . 您的列表中存在一个项目,该项目的属性返回null It is not the first one. 这不是第一个。 .Any() and First() will both loop just over the Enumerable until the first element is found which fulfills the condition. .Any()First()都将在Enumerable上循环,直到找到第一个满足条件的元素为止。

ToList() will loop over all elements --> one Attribute returns null and the call of the .ToLower instance method will result in a NullReferenceException ToList()将遍历所有元素->一个Attribute返回null并且.ToLower实例方法的调用将导致NullReferenceException

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用XML.LINQ(C#)帮助阅读递归XML - Help reading recursive XML using XML.LINQ (C#) 对象引用未设置为LINQ C#中的对象实例 - Object reference not set to an instance of an object in LINQ C# 对象引用未设置为对象xml C#的实例,并使用C#在xml文件中添加一些节点 - object reference not set to an instance of an object xml C# and adding some node in xml file using C# c# linq 异常:未将对象引用设置为对象的实例 - c# linq exception: object reference not set to an instance of an object &#39;你调用的对象是空的。&#39; 在C#linq to SQL中 - 'Object reference not set to an instance of an object.' in C# linq to SQL C#XML解析错误:对象引用未设置为对象的实例 - C# XML Parsing Error: Object reference not set to an instance of an object Gettting对象引用未设置为对象的实例。 使用LINQ C#数据表angularjs? - Gettting Object reference not set to an instance of an object. using linq c# datatable angularjs? C#和XML-对象引用未设置为对象的实例 - C# & XML - Object reference not set to an instance of an object Linq从XML到具有一个元素ToList &lt;&gt;的对象 - Linq from XML to Object with one Element ToList<> C# ASP.NET:“对象引用未设置为 object 的实例。” System.Xml.Linq.XElement.Attribute(...) 返回 null - C# ASP.NET: 'Object reference not set to an instance of an object.' System.Xml.Linq.XElement.Attribute(…) returned null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM