Hi, in a ASP.NET application (MVC) I have a foreach loop that loops through a structure that may contain or not some element:
<% foreach (XElement segnalazione in ((XElement)ViewData["collezioneSegnalazioni"]).Elements("dossier")) { %>
<tr>
<td><%= Html.Encode(segnalazione.Element("NUM_DOSSIER").Value) %></td>
<td><%= Html.Encode(segnalazione.Element("ANAG_RAGSOC_CGN").Value) %></td>
<td><%= Html.Encode(segnalazione.Element("ID_RIFATT_SEGN0").Value) %></td>
<td><%= Html.Encode(segnalazione.Element("FLG_STATUS").Value) %></td>
<td><%= Html.Encode(segnalazione.Element("DT_ACCADIMENTO").Value)%></td>
<td><%= Html.Encode(segnalazione.Element("COD_RAMO_LUNA").Value) %></td>
</tr>
<% } %>
Now, I get a NullReferenceException when Element("DT_ACCADIMENTO") is not set within the XElement. Is there a quick way to handle this? I tried with
<td><%= Html.Encode(segnalazione.Element("DT_ACCADIMENTO").Value ?? "")%></td>
but it does not work as, I guess, it checks if Value is null, where I have a problem with the field itself. Any help appriciated