XElement.SetValue(Object) 方法

定义

设置此元素的值。Sets the value of this element.

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

例外

valuenullThe value is null.

value 是一个 XObjectThe value is an XObject.

示例

下面的示例创建一个包含子元素的元素。The following example creates an element that contains a child element. 然后,它使用此方法设置元素的值。It then sets the value of the element using this method.

XElement root = new XElement("Root",  
    new XElement("Child", "child content")  
);  
root.SetValue("new content");  
Console.WriteLine(root);  
Dim root As XElement = _   
        <Root>  
            <Child>child content</Child>  
        </Root>  
  
root.SetValue("new content")  
Console.WriteLine(root)  

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

<Root>new content</Root>  

注解

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

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

适用于

另请参阅