XElement.Value 属性

定义

获取或设置此元素的串连文本内容。Gets or sets the concatenated text contents of this element.

public:
 property System::String ^ Value { System::String ^ get(); void set(System::String ^ value); };
public string Value { get; set; }
member this.Value : string with get, set
Public Property Value As String

属性值

一个包含此元素所有文本内容的 StringA String that contains all of the text content of this element. 如果有多个文本节点,则将其串连起来。If there are multiple text nodes, they will be concatenated.

示例

下面的示例使用此属性来检索包含混合内容的元素的文本。The following example uses this property to retrieve the text of an element with mixed content.

XElement el = XElement.Parse("<Root>This is <b>mixed</b> content</Root>");  
Console.WriteLine("{0}", el.Value);  
Dim el As XElement = <Root>This is <b>mixed</b> content</Root>  
Console.WriteLine("{0}", el.Value)  

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

This is mixed content  

注解

使用此属性,可获取或设置元素的值。Using this property, you can get or set the value of an element.

设置此属性将引发 ChangedChanging 事件。Setting this property will raise the Changed and the Changing events.

如果要获取某个元素的值,但不确定该元素是否存在,更方便的方法是使用显式转换运算符,并将该元素分配给可以为 null 的类型(如 stringNullable<T> Int32If you want to get the value of an element but you are not sure that it exists, it is more convenient to use the explicit conversion operators, and assign the element to a nullable type such as string or Nullable<T> of Int32. 如果该元素不存在,则将可为 null 的类型设置为 nullIf the element does not exist, the nullable type is set to null. 相反,如果要使用此属性,则在访问此属性之前,必须确保 Element 方法不会返回 nullBy contrast, if you want to use this property, you must make sure that the Element method does not return null before you access this property.

适用于

另请参阅