It turns out that for whatever the reason to the constructor for XAttribute does not accept null values for the value parameter. I think this makes sense even though the XElement constructor will accept null values for the value parameter – mostly because it can still function; a null value for an attribute would be nothing not ‘attributeName=”"‘.
The quick and dirty: there’s an easy solution, use an extension method.
[code lang="csharp"] public static class AddXAttribute
{
public static XElement NewXAttribute(this XElement xa, XName name, Object value)
{
if (xa != null && value != null)
{
xa.Add(new XAttribute(name, value));
}
return xa;
}
}[/code]