在将其添加到匿名对象之前,如何检查此XElement是否不为null? [英] How can i check if this XElement is not null before adding it to an anonymous object?

查看:79
本文介绍了在将其添加到匿名对象之前,如何检查此XElement是否不为null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从XML文件填充匿名对象.到现在为止,

I'm populating an anonymous object from an XML file. Up until now,

commentary.Elements("Commentator")

一直都有一个值,所以我从来不需要检查null.不过,我不得不删除了它,现在它在尝试读取该行时失败了.

has always had a value so i've never had to check for null. I've had to remove that though, and now it's failing when it tries to read that line.

我正在查看代码,但是我不知道要更改什么,因为它被选择为匿名对象的属性.

I'm looking at the code and I don't know what to change though, because its being selected into a property of an anonymous object.

var genericOfflineFactsheet = new
    {
        Commentary = (from commentary in doc.Elements("Commentary")
                      select new
                      {
                          CommentaryPage = (string)commentary.Attribute("page"),
                          BusinessName = (string)commentary.Attribute("businessName"),
                          Commentator = (from commentator in commentary.Elements("Commentator")
                                         select new CommentatorPanel // ASP.NET UserControl
                                         {
                                             CommentatorName = (string)commentator.Attribute("name"),
                                             CommentatorTitle = (string)commentator.Attribute("title"),
                                             CommentatorCompany = (string)commentator.Attribute("company")
                                         }).FirstOrDefault()
                      }).FirstOrDefault()

问题是,我无法完全删除该行,因为有时commentary.Elements("Commentator") 确实具有值.我敢肯定这个问题已经解决过,但是我看不到该怎么办.有什么想法吗?

The thing is, I can't completely remove the line because sometimes commentary.Elements("Commentator") does have a value. I'm sure this issue has been dealt with before, but I can't see what to do. Any ideas?

推荐答案

只需添加一个空检查. (下面的代码应该是一个很好的测试者.有一个有注释者,有一个没有注释者)

Just add a null check. (code below should be a good tester. Has one with and one without Commentator)

XDocument doc = new XDocument(
    new XDeclaration("1.0", "utf-8", "yes"),
    new XComment("Example"),
new XElement("Commentarys",
        new XElement("Commentary",
            new XAttribute("page", "2"),
            new XAttribute("businessName", "name"),
            new XElement("Commentator",
               new XAttribute("name", "name"),
                new XAttribute("title", "title")
            )
         )
        ,
        new XElement("Commentary",
            new XAttribute("page", "3"),
            new XAttribute("businessName", "name2")

            )
    )
    );
var genericOfflineFactsheet = new
{
    Commentary = (
           from commentary in doc.Elements()
                .First().Elements("Commentary")
           select new
          {
              CommentaryPage = (string)commentary.Attribute("page"),
              BusinessName = (string)commentary.Attribute("businessName"),
              Commentator = (from commentator in commentary.Elements("Commentator")
                             where commentator != null //<-----you need to add this line
                             select new // ASP.NET UserControl
                             {
                                 CommentatorName = (string)commentator.Attribute("name"),
                                 CommentatorTitle = (string)commentator.Attribute("title"),
                                 CommentatorCompany = (string)commentator.Attribute("company")
                             }

                             ).FirstOrDefault()
          }
 ).FirstOrDefault()
};

这篇关于在将其添加到匿名对象之前,如何检查此XElement是否不为null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆