Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading XML using XDocument & Linq - check if element is NULL?

Tags:

I'm using LINQ together with XDocument to read a XML File. This is the code:

XDocument xml = XDocument.Load(filename);  var q = from b in xml.Descendants("product")         select new         {             name = b.Element("name").Value,             price = b.Element("price").Value,                                 extra = b.Element("extra1").Value,             deeplink = b.Element("deepLink").Value                            }; 

Now the problem is, the extra1 field is not always present. There are items in the XML file without that node. If that happens it's crashing with a NullReferenceException.

Is there any possibility to include a "check if null" so I can prevent it from crashing?

like image 460
adchased Avatar asked Apr 07 '10 17:04

adchased


People also ask

How do I load XML in XDocument?

One possible use for this method is to create a copy of a DOM document in a LINQ to XML tree. To do this, you create an XmlNodeReader from a DOM document, and then use the XmlNodeReader to create an XDocument. LINQ to XML's loading functionality is built upon XmlReader.

What is XDocument parse?

Parse(String) Creates a new XDocument from a string. Parse(String, LoadOptions) Creates a new XDocument from a string, optionally preserving white space, setting the base URI, and retaining line information.

What is the difference between XmlDocument and XDocument?

XDocument is from the LINQ to XML API, and XmlDocument is the standard DOM-style API for XML. If you know DOM well, and don't want to learn LINQ to XML, go with XmlDocument . If you're new to both, check out this page that compares the two, and pick which one you like the looks of better.

What is XDocument C#?

The XDocument class contains the information necessary for a valid XML document, which includes an XML declaration, processing instructions, and comments. You only have to create XDocument objects if you require the specific functionality provided by the XDocument class.


2 Answers

Use (string) instead of .Value:

var q = from b in xml.Descendants("product")         select new         {             name = (string)b.Element("name"),             price = (double?)b.Element("price"),                                 extra = (string)b.Element("extra1"),             deeplink = (string)b.Element("deepLink")                            }; 

This also works with other datatypes, including many nullable types in case the element is not always present.

like image 166
dtb Avatar answered Sep 20 '22 20:09

dtb


You can use "null coalescing" operator:

var q = from b in xml.Descendants("product")         select new         {             name = (string)b.Element("name") ?? "Default Name",             price = (double?)b.Element("price") ?? 0.0,                                 extra = (string)b.Element("extra1") ?? String.Empty,             deeplink = (string)b.Element("deepLink") ?? String.Empty                            }; 

This way, you will have full control about default value used when there's no element.

like image 34
MarekBaron Avatar answered Sep 17 '22 20:09

MarekBaron