共用方式為


XML Value 屬性

更新:2007 年 11 月

提供存取 XElement 物件的集合中第一個項目的值。

object.Value

參數

  • object
    必要項。XElement 物件的集合。

傳回值

String,包含集合中第一個項目的值,如果該集合是空的則為 Nothing。

備註

Value 屬性 (Property) 可讓您輕鬆地存取 XElement 物件的集合中第一個項目的值。這個屬性 (Property) 會先檢查集合中是否至少包含一個物件。如果集合是空的,則這個屬性 (Property) 會傳回 Nothing。否則,這個屬性 (Property) 會傳回集合中第一個項目的 Value 屬性 (Property) 值。

注意事項:

當您使用 '@' 識別項存取 XML 屬性 (Attribute) 的值時,屬性 (Attribute) 值會以 String 傳回,您不需要明確指定 Value 屬性 (Property)。

若要存取集合中的其他項目,您可以使用 XML 擴充索引子屬性 (Property)。如需詳細資訊,請參閱擴充索引子屬性

繼承

大多數的使用者不需要實作 IEnumerable<T>,因此可以忽略本節的內容。

The Value 屬性 (Property) 是一個擴充屬性,適用於會實作 IEnumerable(Of XElement) 的型別。繫結這個擴充屬性就像繫結擴充方法:如果有型別實作其中一個介面並定義了名為 "Value" 的屬性 (Property),則該屬性 (Property) 會優先於擴充屬性。換句話說,只要在實作 IEnumerable(Of XElement) 的類別 (Class) 中定義新的屬性 (Property),即可覆寫這個 Value 屬性 (Property)。

範例

下列範例顯示如何使用 Value 屬性 (Property) 存取 XElement 物件之集合中的第一個節點。這個範例會使用子項軸屬性 (Property) 取得 contact 物件中所有名為 phone 之子節點的集合。

Dim contact As XElement = _
    <contact>
        <name>Patrick Hines</name>
        <phone type="home">206-555-0144</phone>
        <phone type="work">425-555-0145</phone>
    </contact>

Console.WriteLine("Phone number: " & contact.<phone>.Value)

這個程式碼會顯示下列文字:

Phone number: 206-555-0144

下列範例顯示如何從 XAttribute 物件的集合中取得 XML 屬性 (Attribute) 的值。這個範例會使用屬性 (Attribute) 軸屬性 (Property) 顯示所有 phone 項目之 type 屬性 (Attribute) 的值。

Dim contact As XElement = _
    <contact>
      <name>Patrick Hines</name>
      <phone type="home">206-555-0144</phone>
      <phone type="work">425-555-0145</phone>
    </contact>


Dim types = contact.<phone>.Attributes("type")

For Each attr In types
  Console.WriteLine(attr.Value)
Next

這個程式碼會顯示下列文字:

home

work

請參閱

概念

擴充方法 (Visual Basic)

參考

XElement

IEnumerable<T>

擴充索引子屬性

XML 子代軸屬性

XML 屬性軸屬性

其他資源

XML 軸屬性

XML 常值

在 Visual Basic 中建立 XML