views:

35

answers:

3

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

commentary.Elements("Commentator")

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()

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?

+1  A: 

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()
};
Nix
A: 

Untested...

how about something like:

  XElement xe = doc.Elements("Commentary").FirstOrDefault();
  Commentary = xe == null ? null :
    select new { ....snip....
Tim Jarvis
A: 

This is a case where I might consider the ?? (coalesce) operator.

null ?? x --> x

In this case you could get away with coalescing to an empty Enumerable.

pst