2

我正在从 XML 文件填充匿名对象。直到现在,

commentary.Elements("Commentator")

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

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

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") 确实有价值。我确定这个问题之前已经处理过,但我不知道该怎么做。有任何想法吗?

4

4 回答 4

2

只需添加一个空检查。(下面的代码应该是一个很好的测试器。有一个有评论员,一个没有评论员)

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()
};
于 2010-10-12T21:06:27.410 回答
1

未经测试...

怎么样:

  XElement xe = doc.Elements("Commentary").FirstOrDefault();
  Commentary = xe == null ? null :
    select new { ....snip....
于 2010-10-12T21:15:35.937 回答
1

这是我可以考虑的情况??(合并)运算符。

null ?? x --> x

在这种情况下,您可以通过合并到一个空的 Enumerable 来侥幸成功。

于 2010-10-12T21:24:48.583 回答
0

我用 ?运算符以这种方式防止生成 XMLElement:

  • 如果我使用的对象不为空,我会在数组中返回一个新的 XElement
  • 如果它为空,我返回一个 Enumerable.Empty

例子:

var xml = new XElement("Root",
    myobject == null ? Enumerable.Empty<XElement>() : <= empty IEnumerable if it is null
                       new []                         <= a array with a XElement
                           { 
                               new XElement("myobject", 
                                   new XAttribute("Name", myobject.Name),
                                   new XAttribute("Type", myobject.Type)
                               ...)
                           },
    ...);

在这种情况下,如果创建 XElement 所涉及的对象为空,则不会生成 XElement。

于 2013-04-19T05:45:35.773 回答