I am trying to read an xml file and get attribute but sometime this attribute doesn't exist.
When it doesn't exist, I get this error :
System.Linq.Enumerable+WhereSelectEnumerableIterator
2[System.Xml.Linq.XElement,<>f__AnonymousType02[System.String,System.String]]
And :
Critical Error : System.NullReferenceException:....
My code :
string url = @"http://vigilance.meteofrance.com/data/NXFR33_LFPW_.xml";
XDocument doc = XDocument.Load(url);
var selectedBook = from r in doc.Descendants("DV")
.Where(r => (string)r.Attribute("dep").Value == Departement)
select new
{
Color = r.Attribute("coul").Value,
Risque = (string) r.Element("risque").Attribute("val").Value,
};
And the XML looks like this:
<DV dep="02" coul="1"/>
<DV dep="03" coul="3">
<risque val="6"/>
</DV>
Does anyone have an idea ?