当.count()大于0时,从.select()中获取NullReferenceException
本文关键字:select 获取 NullReferenceException 0时 count 大于 | 更新日期: 2023-09-27 18:18:35
我有一个XML文件:
<Org href="https://vcloudserver/api/v1.0/org/272521719" type="application/vnd.vmware.vcloud.org+xml" name="blah-blah" xmlns="http://www.vmware.com/vcloud/v1">
<Link href="https://vcloudserver/api/v1.0/vdc/1093121285" type="application/vnd.vmware.vcloud.vdc+xml" name="blah-haha" rel="down"/>
<Link href="https://vcloudserver/api/v1.0/vdc/1213262741" type="application/vnd.vmware.vcloud.vdc+xml" name="blah-hoho" rel="down"/>
<Link href="https://vcloudserver/api/v1.0/tasksList/272521719" type="application/vnd.vmware.vcloud.tasksList+xml" rel="down"/>
<Link href="https://vcloudserver/api/v1.0/catalog/1309520800" type="application/vnd.vmware.vcloud.catalog+xml" name="blah-hehe" rel="down"/>
<Link href="https://vcloudserver/api/v1.0/org/272521719/catalog/1309520800/controlAccess/" type="application/vnd.vmware.vcloud.controlAccess+xml" rel="down"/>
<Link href="https://vcloudserver/api/v1.0/org/272521719/catalog/1309520800/action/controlAccess" type="application/vnd.vmware.vcloud.controlAccess+xml" rel="controlAccess"/>
<Link href="https://vcloudserver/api/v1.0/network/1435818199" type="application/vnd.vmware.vcloud.network+xml" name="blah-whodat" rel="down"/>
<Link href="https://vcloudserver/api/v1.0/network/2048048931" type="application/vnd.vmware.vcloud.network+xml" name="blah-disis" rel="down"/>
<Description/>
<FullName>Blah diddy Blah-Blah</FullName>
</Org>
那么,给定以下内容:
XNamespace nameSpace = "http://www.vmware.com/vcloud/v1";
var doc = XDocument.Parse(xml);
当我在VS即时窗口中调试以下代码时:
doc.Root.Elements(nameSpace + "Link").Count()
得到一个期望值,8。但是当我进一步使用:
var vdcs = doc.Root.Elements(nameSpace + "Link")
.Select(x => new vDC()
{
Name = x.Attribute("name").Value,
Type = x.Attribute("type").Value,
Href = x.Attribute("href").Value
}).Where(x=>x.Type.Contains("vdc"));
我得到一个NullReferenceException试图访问vdcs.Count()。我已经在这上面折腾太久了……我在其他地方也用得很好,所以这没什么用。:(尝试在Where()之前填充一个ToList(),并且只是将NullReferenceException移动到该调用。
如果有帮助,vDC当前定义为:
public class vDC
{
public string Name { get; set; }
public string Type { get; set; }
public string Href { get; set; }
}
部分Link
元素没有name
属性,所以:
var vdcs = doc.Root.Elements(nameSpace + "Link")
.Select(x => new vDC()
{
Name = x.Attribute("name").Value,
Type = x.Attribute("type").Value,
Href = x.Attribute("href").Value
}).Where(x=>x.Type.Contains("vdc"));
窒息
Name = x.Attribute("name").Value
因为在某些情况下x.Attribute("name")
是null
。
var vdcs = doc.Root.Elements(nameSpace + "Link")
.Select(x => new
{
NameAttr = x.Attribute("name"),
TypeAttr = x.Attribute("type"),
HrefAttr = x.Attribute("href")
}).Select(x => new vDC()
{
Name = x.NameAttr == null ? null : x.NameAttr.Value,
Type = x.TypeAttr == null ? null : x.TypeAttr.Value,
Href = x.HrefAttr == null ? null : x.HrefAttr.Value,
}).Where(x=>x.Type.Contains("vdc"));
你的一些链接没有name
-他们有rel
代替。NPE就是这样来的。
只有在调用Count
时才会看到它,因为实际的求值在此之前不会发生。
要更改代码以防止NPE,请添加Where
条件:
var vdcs = doc.Root.Elements(nameSpace + "Link")
.Where(x => x.Attribute("name") != null && x.Attribute("type") != null && x.Attribute("value") != null)
.Select(x => new vDC {
Name = x.Attribute("name").Value,
Type = x.Attribute("type").Value,
Href = x.Attribute("href").Value
}).Where(x=>x.Type.Contains("vdc"));