当.Count()大于零LINQ to XML时从.Select()获取NullReferenceException

时间:2011-12-23 02:38:27

标签: c# xml linq linq-to-xml

我有一个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()

我得到了预期值,但是当我使用以下内容进一步讨论时:

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"));

我在尝试访问vdcs.Count()时遇到NullReferenceException。我一直在搞乱这个问题......我还有其他地方这种类型的东西工作正常,所以这没有用。 :(尝试在Where()之前填充ToList(),并将NullReferenceException移动到该调用。

如果有帮助,vDC目前定义为:

public class vDC
{
  public string Name { get; set; }
  public string Type { get; set; }
  public string Href { get; set; }
}

2 个答案:

答案 0 :(得分:2)

某些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"));

或类似的应该解决问题。

答案 1 :(得分:1)

您的部分链接没有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"));