I have a simple xml.
var FieldsInData = from fields in xdoc.Descendants("DataRecord")
select fields;
Now i have n different XElement items in FildsInData.
foreach (var item in FieldsInData)
{
//working
String id = item.Attribute("id").Value;
//now i get a nullReferenceException because that XElement item has no Attribute called **fail**
String notExistingAttribute = item.Attribute("fail").Value;
}
With that fail attribute i get nullReferenceException, because it is not there. Sometimes it is, sometimes it is not. How do i handle that gracefully?
I tryed using value.SingleOrDefault(); but i get another exception because it is IEnumerable of Char.
You simply need to check for
null: