需要添加XAttribute到的XElement如果元素存在属性
XAttribute newatt = new XAttribute(“TAG”, value);
添加到XElement elem,但elem可能已经包含名为"TAG"属性,所以elem.Add(newatt);会给错误。我目前使用的解决方法是首先要检查:
if (elem.Attribute(“TAG”) != null) // check if attribute exists
elem.SetAttributeValue(“TAG”, newatt.Value.ToString()); // Attribute exists
else
elem.Add(newatt); // Attribute does not exist