共用方式為


XPathNavigator.ValueAsBoolean 屬性

定義

取得目前的節點值做為 Boolean

public:
 virtual property bool ValueAsBoolean { bool get(); };
public override bool ValueAsBoolean { get; }
member this.ValueAsBoolean : bool
Public Overrides ReadOnly Property ValueAsBoolean As Boolean

屬性值

Boolean

目前節點的值,為 Boolean

例外狀況

目前節點的字串值不能轉換為 Boolean

嘗試轉換成 Boolean 無效。

範例

在下列範例中,會使用 ValueAsBooleanValueAsDateTimeValueAsDoubleValueAsIntValueAsLong 屬性傳回檔案中 valueas.xml 每個元素的值。

XPathDocument^ document = gcnew XPathDocument("valueas.xml");
XPathNavigator^ navigator = document->CreateNavigator();

// ValueAsBoolean
navigator->MoveToChild("root", "");
navigator->MoveToChild("booleanElement", "");
bool^ booleanValue = navigator->ValueAsBoolean;
Console::WriteLine(navigator->LocalName + ": " + booleanValue);

// ValueAsDateTime
navigator->MoveToNext("dateTimeElement", "");
DateTime^ dateTimeValue = navigator->ValueAsDateTime;
Console::WriteLine(navigator->LocalName + ": " + dateTimeValue);

// ValueAsDouble, ValueAsInt32, ValueAsInt64, ValueAsSingle
navigator->MoveToNext("numberElement", "");
Double doubleValue = navigator->ValueAsDouble;
Int32 int32Value = navigator->ValueAsInt;
Int64 int64Value = navigator->ValueAsLong;
Console::WriteLine(navigator->LocalName + ": " + doubleValue);
Console::WriteLine(navigator->LocalName + ": " + int32Value);
Console::WriteLine(navigator->LocalName + ": " + int64Value);
XPathDocument document = new XPathDocument("valueas.xml");
XPathNavigator navigator = document.CreateNavigator();

// ValueAsBoolean
navigator.MoveToChild("root", "");
navigator.MoveToChild("booleanElement", "");
bool booleanValue = navigator.ValueAsBoolean;
Console.WriteLine(navigator.LocalName + ": " + booleanValue);

// ValueAsDateTime
navigator.MoveToNext("dateTimeElement", "");
DateTime dateTimeValue = navigator.ValueAsDateTime;
Console.WriteLine(navigator.LocalName + ": " + dateTimeValue);

// ValueAsDouble, ValueAsInt32, ValueAsInt64, ValueAsSingle
navigator.MoveToNext("numberElement", "");
Double doubleValue = navigator.ValueAsDouble;
Int32 int32Value = navigator.ValueAsInt;
Int64 int64Value = navigator.ValueAsLong;
Console.WriteLine(navigator.LocalName + ": " + doubleValue);
Console.WriteLine(navigator.LocalName + ": " + int32Value);
Console.WriteLine(navigator.LocalName + ": " + int64Value);
Dim document As XPathDocument = New XPathDocument("valueas.xml")
Dim navigator As XPathNavigator = document.CreateNavigator()

' ValueAsBoolean
navigator.MoveToChild("root", "")
navigator.MoveToChild("booleanElement", "")
Dim booleanValue As Boolean = navigator.ValueAsBoolean
Console.WriteLine(navigator.LocalName + ": " + booleanValue)

' ValueAsDateTime
navigator.MoveToNext("dateTimeElement", "")
Dim dateTimeValue As DateTime = navigator.ValueAsDateTime
Console.WriteLine(navigator.LocalName + ": " + dateTimeValue)

' ValueAsDouble, ValueAsInt32, ValueAsInt64, ValueAsSingle
navigator.MoveToNext("numberElement", "")
Dim doubleValue As Double = navigator.ValueAsDouble
Dim int32Value As Int32 = navigator.ValueAsInt
Dim int64Value As Int64 = navigator.ValueAsLong
Console.WriteLine(navigator.LocalName + ": " + doubleValue)
Console.WriteLine(navigator.LocalName + ": " + int32Value)
Console.WriteLine(navigator.LocalName + ": " + int64Value)

範例將 valueas.xml 檔案做為輸入。

<root>  
    <booleanElement>true</booleanElement>  
    <dateTimeElement>2004-04-20T12:00:00</dateTimeElement>  
    <decimalElement>10.00</decimalElement>  
    <numberElement>100000000</numberElement>  
</root>  

備註

XPathNavigator例如,如果 具有架構或型別資訊 (,則從 XmlDocument 使用驗證 XmlReader) XML 架構初始化的物件,而且目前的節點定義為 XML 架構 xs:boolean 類型,則 ValueAsBoolean 屬性會傳回目前節點的值做為 unboxed Boolean 物件。

不過,如果 XPathNavigator 沒有任何架構或類型資訊,屬性 ValueAsBoolean 會根據 的 XPath 2.0 轉換規則 xs:boolean ,嘗試將目前節點 Boolean 的字串值轉換為值。

適用於