【发布时间】:2017-07-19 10:34:57
【问题描述】:
需要补充
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
是否有更短的方法来完成这项任务,也许已经可用的 XElement 函数检查现有的 "TAG" 可能(我知道可以将上述 sn-p 包装到函数中)?
【问题讨论】: