XElement.Value 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定此元素的串接文字內容。
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
屬性值
String A 包含了該元素的所有文字內容。 如果有多個文字節點,它們會被串接。
範例
以下範例利用此特性來取得混合內容元素的文本。
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 is mixed content
備註
利用這個特性,你可以取得或設定元素的值。
如果你想取得某個元素的值,但不確定它是否存在,使用顯式轉換運算子,並將該元素指派到可空的類型,例如 stringNullable<T> 或 , Int32會更方便。 若該元素不存在,則可空的型別設為 null。 相較之下,若你想使用此屬性,必須確保 Element 在存取此屬性前該方法不會回傳 null 。