Need to add
XAttribute newatt = new XAttribute(\"TAG\", value);
to XElement elem
, but the elem
could already contai
You don't need to check whether the attribute already exists before using SetAttributeValue
. Just:
// Unconditional
elem.SetAttributeValue("TAG", value);
(There's no point even creating the XAttribute
yourself.)
From the documentation:
The value is assigned to the attribute with the specified name. If no attribute with the specified name exists, a new attribute is added. If the value is null, the attribute with the specified name, if any, is deleted.