XAttribute.SetValue(Object) 方法

定义

设置此属性的值。Sets the value of this attribute.

public:
 void SetValue(System::Object ^ value);
public void SetValue (object value);
member this.SetValue : obj -> unit
Public Sub SetValue (value As Object)

参数

value
Object

分配到此属性的值。The value to assign to this attribute.

例外

value 参数为 nullThe value parameter is null.

value 是一个 XObjectThe value is an XObject.

示例

下面的示例创建一个具有多个属性的元素。The following example creates an element with several attributes. 然后,它会更改其中一个属性的内容。It then changes the contents of one of the attributes.

XElement root = new XElement("Root",  
    new XAttribute("Att1", "content1"),  
    new XAttribute("Att2", "content2"),  
    new XAttribute("Att3", "content3")  
);  
XAttribute att = root.Attribute("Att2");  
att.SetValue("new content");  
Console.WriteLine(root);  
Dim root As XElement = <Root Att1="content1" Att2="content2" Att3="content3"/>  
Dim att As XAttribute = root.Attribute("Att2")  
att.SetValue("new content")  
Console.WriteLine(root)  

该示例产生下面的输出:This example produces the following output:

<Root Att1="content1" Att2="new content" Att3="content3" />  

注解

将值转换为其字符串表示形式,并将此值分配给 Value 属性 (Property)。The value is converted to its string representation and assigned to the Value property.

传递派生自的类的实例( XObject 如或)是无效的 XElement XAttributeIt is invalid to pass an instance of a class that derives from XObject, such as XElement or XAttribute.

此方法将引发 ChangedChanging 事件。This method will raise the Changed and the Changing events.

适用于

另请参阅