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?
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With