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 的强制转换无效。

示例

In the following example, the value of each element in the valueas.xml file is returned using the ValueAsBoolean, ValueAsDateTime, ValueAsDouble, ValueAsInt, and ValueAsLong properties.

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此属性将返回当前节点的值作为未装箱Boolean对象。

但是,如果XPathNavigator没有任何架构或类型信息,则ValueAsBoolean属性会根据 XPath 2.0 强制转换规则xs:boolean尝试将当前节点的字符串值转换为Boolean值。

适用于