XElement.SetElementValue(XName, Object) 方法

定义

设置子元素的值、添加子元素或移除子元素。Sets the value of a child element, adds a child element, or removes a child element.

public:
 void SetElementValue(System::Xml::Linq::XName ^ name, System::Object ^ value);
public void SetElementValue (System.Xml.Linq.XName name, object value);
public void SetElementValue (System.Xml.Linq.XName name, object? value);
member this.SetElementValue : System.Xml.Linq.XName * obj -> unit
Public Sub SetElementValue (name As XName, value As Object)

参数

name
XName

一个 XName,其中包含要更改的子元素的名称。An XName that contains the name of the child element to change.

value
Object

要分配给子元素的值。The value to assign to the child element. 如果值为 null,则移除子元素。The child element is removed if the value is null. 否则,会将值转换为其字符串表示形式,并将该值分配给子元素的 Value 属性。Otherwise, the value is converted to its string representation and assigned to the Value property of the child element.

例外

valueXObject 的一个实例。The value is an instance of XObject.

示例

下面的示例创建一个具有子元素的元素。The following example creates an element with a child element. 然后,它使用此方法设置子元素的值。It then uses this method to set the value of the child element.

// Create an element with no content  
XElement root = new XElement("Root");  

// Add some name/value pairs.  
root.SetElementValue("Ele1", 1);  
root.SetElementValue("Ele2", 2);  
root.SetElementValue("Ele3", 3);  
Console.WriteLine(root);  

// Modify one of the name/value pairs.  
root.SetElementValue("Ele2", 22);  
Console.WriteLine(root);  

// Remove one of the name/value pairs.  
root.SetElementValue("Ele3", null);  
Console.WriteLine(root);  
' Create an element with no content.  
Dim root As XElement = <Root/>  

' Add some name/value pairs.  
root.SetElementValue("Ele1", 1)  
root.SetElementValue("Ele2", 2)  
root.SetElementValue("Ele3", 3)  
Console.WriteLine(root)  

' Modify one of the name/value pairs.  
root.SetElementValue("Ele2", 22)  
Console.WriteLine(root)  

' Remove one of the name/value pairs.  
root.SetElementValue("Ele3", Nothing)  
Console.WriteLine(root)  

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

<Root>  
  <Ele1>1</Ele1>  
  <Ele2>2</Ele2>  
  <Ele3>3</Ele3>  
</Root>  
<Root>  
  <Ele1>1</Ele1>  
  <Ele2>22</Ele2>  
  <Ele3>3</Ele3>  
</Root>  
<Root>  
  <Ele1>1</Ele1>  
  <Ele2>22</Ele2>  
</Root>  

注解

此方法旨在使名称/值对的列表易于维护为一组子元素。This method is designed to make it easy to maintain a list of name/value pairs as a set of children elements. 维护列表时,需要添加对、修改对或删除对。When maintaining the list, you need to add pairs, modify pairs, or delete pairs. 如果调用此方法时将不存在的名称传递为子元素,则此方法将为您创建一个子元素。If you call this method passing a name that does not exist as a child element, this method creates a child element for you. 如果调用此方法来传递现有子元素的名称,则此方法将子元素的值修改为你指定的值。If you call this method passing the name of an existing child element, this method modifies the value of the child element to the value that you specify. 如果传递 null ,则 value 此方法将移除子元素。If you pass null for value, this method removes the child element.

此方法将引发事件。This method will raise events.

该值将分配给具有指定名称的第一个子元素。The value is assigned to the first child element with the specified name. 如果不存在具有指定名称的子元素,则添加新的子元素。If no child element with the specified name exists, a new child element is added. 如果该值为 null,则删除具有指定名称的第一个子元素(如果有)。If the value is null, the first child element with the specified name, if any, is deleted.

此方法不会将子节点或特性添加到指定的子元素。This method does not add child nodes or attributes to the specified child element. 如果派生自的任何对象作为传递,则此方法将引发异常 XObject valueThis method throws an exception if any object that derives from XObject is passed as value.

有关详细信息,请参阅 维护名称/值对For more information, see Maintain name-value pairs.

适用于

另请参阅