Add the XAttribute to XElement if attribute exists in the element

前端 未结 1 786
失恋的感觉
失恋的感觉 2021-01-24 01:18

Need to add

XAttribute newatt = new XAttribute(\"TAG\", value); 

to XElement elem, but the elem could already contai

1条回答
  •  粉色の甜心
    2021-01-24 01:58

    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.

    0 讨论(0)
提交回复
热议问题