How to: Check for nulls when using LINQ to XML

Posted Wednesday, February 6, 2008 4:54 PM by CoreyRoth

I continue to work with LINQ to XML, and I thought this might be worth mentioning (although it is somewhat common sense).  An issue often when working with XML attribute (or elements) is that they might not always exists (i.e.: they are null).  Therefore, you need to check for this.  Specifically, this is an issue when you are assigning attributes into a new anonymous type (although it could occur using a regular type as well).  Consider the following example.  What if MyColumn is not present in some of the Item elements in the XML document?  The code would end up throwing an exception when you tried to enumerate items.

var items = from item in assetTypes.Elements("Item")                      select new                      


   Name = item.Attribute("Name").Value,                          

   MyColumn = item.Attribute("MyColumn").Value


How do you fix it?  First you use the Any() method of the attribute to see if any of that attribute exist.  Then it is just a matter of using shorthand if/then syntax.  Just replace MyColumn with the code below.

MyColumn = item.Attributes("MyColumn").Any() ? item.Attribute("MyColumn") : null

As you can see it is relatively simple, once you know to use Any() to look for the existance of an attribute. On a related note you can apply the same techique to see if an element exists.

Filed under:


# re: How to: Check for nulls when using LINQ to XML

Tuesday, June 1, 2010 12:15 PM by Mithun

Perfect, your was the only site that I found had to the point solution to this problem! Took me hours to find it but eventually got there :-) Cheers - Mithun

# re: How to: Check for nulls when using LINQ to XML

Wednesday, August 17, 2011 12:06 AM by Ramesh

Excellent work. Thanks lot for your help.

# Silverlight 4 を使用した SharePoint の高度な検索アプリケーションを作成する

Monday, March 12, 2012 4:10 PM by Office 365 の技術系ブログ

対象: Office 365 for Enterprise Grid には、重要な情報を持つ Office 365 の専門家が多数含まれています。 Grid のユーザー投稿ブログ シリーズ

# re: How to: Check for nulls when using LINQ to XML

Thursday, May 23, 2013 8:13 AM by ATJ

Hello, I can't find the .Any()  method in my attribute.

Can you help me please ? I need it for the Warranty and description.

                   XDocument product = XDocument.Load(info1.ProductID.ToString() + ".xml");

                   var productData = from item in product.Descendants("Product")

                                     select new


                                       HighPic = item.Attribute("HighPic").Value,

                                       LowPic = item.Attribute("LowPic").Value,

                                       Name = item.Attribute("Name").Value,

                                       Description = item.Element("ProductDescription").Attribute("LongDesc").Value,

                                       Warranty = item.Element("ProductDescription").Attribute("WarrantyInfo").Value,

                                       Prod_id = item.Attribute("Prod_id").Value,

                                       ReleaseDate = item.Attribute("ReleaseDate").Value,

                                       Title = item.Attribute("Title").Value,

                                       CategoryName = item.Element("Category").Element("Name").Attribute("Value").Value,

                                       CategoryID = item.Element("Category").Element("Name").Attribute("ID").Value


Leave a Comment