共用方式為


XPathNavigator.ValueType 屬性

定義

取得目前節點的 .NET Type 類型。

public:
 virtual property Type ^ ValueType { Type ^ get(); };
public override Type ValueType { get; }
member this.ValueType : Type
Public Overrides ReadOnly Property ValueType As Type

屬性值

Type

目前節點的 .NET Type 。 預設值是 String

範例

在下列範例中 contosoBooks.xml ,會使用 XML 檔案和 contosoBooks.xsd XML 架構定義語言 (XSD) 架構來建立 XPathNavigator 物件。 專案的具型別值 price 會使用 ValueType 屬性顯示,然後使用 方法傳回為字串 ValueAs

// Create an XmlReaderSettings object with the contosoBooks.xsd schema.
XmlReaderSettings^ settings = gcnew XmlReaderSettings();
settings->Schemas->Add("http://www.contoso.com/books", "contosoBooks.xsd");
settings->ValidationType = ValidationType::Schema;

// Create an XmlReader object with the contosoBooks.xml file and its schema.
XmlReader^ reader = XmlReader::Create("contosoBooks.xml", settings);

XPathDocument^ document = gcnew XPathDocument(reader);
XPathNavigator^ navigator = document->CreateNavigator();

navigator->MoveToChild("bookstore", "http://www.contoso.com/books");
navigator->MoveToChild("book", "http://www.contoso.com/books");
navigator->MoveToChild("price", "http://www.contoso.com/books");

// Display the current type of the price element.
Console::WriteLine(navigator->ValueType);

// Get the value of the price element as a string and display it.
String^ price = dynamic_cast<String^>(navigator->ValueAs(String::typeid));
Console::WriteLine(price);
// Create an XmlReaderSettings object with the contosoBooks.xsd schema.
XmlReaderSettings settings = new XmlReaderSettings();
settings.Schemas.Add("http://www.contoso.com/books", "contosoBooks.xsd");
settings.ValidationType = ValidationType.Schema;

// Create an XmlReader object with the contosoBooks.xml file and its schema.
XmlReader reader = XmlReader.Create("contosoBooks.xml", settings);

XPathDocument document = new XPathDocument(reader);
XPathNavigator navigator = document.CreateNavigator();

navigator.MoveToChild("bookstore", "http://www.contoso.com/books");
navigator.MoveToChild("book", "http://www.contoso.com/books");
navigator.MoveToChild("price", "http://www.contoso.com/books");

// Display the current type of the price element.
Console.WriteLine(navigator.ValueType);

// Get the value of the price element as a string and display it.
string price = navigator.ValueAs(typeof(string)) as string;
Console.WriteLine(price);
' Create an XmlReaderSettings object with the contosoBooks.xsd schema.
Dim settings As XmlReaderSettings = New XmlReaderSettings()
settings.Schemas.Add("http://www.contoso.com/books", "contosoBooks.xsd")
settings.ValidationType = ValidationType.Schema

' Create an XmlReader object with the contosoBooks.xml file and its schema.
Dim reader As XmlReader = XmlReader.Create("contosoBooks.xml", settings)

Dim document As XPathDocument = New XPathDocument(reader)
Dim navigator As XPathNavigator = document.CreateNavigator()

navigator.MoveToChild("bookstore", "http://www.contoso.com/books")
navigator.MoveToChild("book", "http://www.contoso.com/books")
navigator.MoveToChild("price", "http://www.contoso.com/books")

' Display the current type of the price element.
Console.WriteLine(navigator.ValueType)

' Get the value of the price element as a string and display it.
Dim price As String = navigator.ValueAs(GetType(String))
Console.WriteLine(price)

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

<?xml version="1.0" encoding="utf-8" ?>  
<bookstore xmlns="http://www.contoso.com/books">  
    <book genre="autobiography" publicationdate="1981-03-22" ISBN="1-861003-11-0">  
        <title>The Autobiography of Benjamin Franklin</title>  
        <author>  
            <first-name>Benjamin</first-name>  
            <last-name>Franklin</last-name>  
        </author>  
        <price>8.99</price>  
    </book>  
    <book genre="novel" publicationdate="1967-11-17" ISBN="0-201-63361-2">  
        <title>The Confidence Man</title>  
        <author>  
            <first-name>Herman</first-name>  
            <last-name>Melville</last-name>  
        </author>  
        <price>11.99</price>  
    </book>  
    <book genre="philosophy" publicationdate="1991-02-15" ISBN="1-861001-57-6">  
        <title>The Gorgias</title>  
        <author>  
            <name>Plato</name>  
        </author>  
        <price>9.99</price>  
    </book>  
</bookstore>  

該範例還採用 contosoBooks.xsd 做為輸入。

<?xml version="1.0" encoding="utf-8"?>  
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.contoso.com/books" xmlns:xs="http://www.w3.org/2001/XMLSchema">  
    <xs:element name="bookstore">  
        <xs:complexType>  
            <xs:sequence>  
                <xs:element maxOccurs="unbounded" name="book">  
                    <xs:complexType>  
                        <xs:sequence>  
                            <xs:element name="title" type="xs:string" />  
                            <xs:element name="author">  
                                <xs:complexType>  
                                    <xs:sequence>  
                                        <xs:element minOccurs="0" name="name" type="xs:string" />  
                                        <xs:element minOccurs="0" name="first-name" type="xs:string" />  
                                        <xs:element minOccurs="0" name="last-name" type="xs:string" />  
                                    </xs:sequence>  
                                </xs:complexType>  
                            </xs:element>  
                            <xs:element name="price" type="xs:decimal" />  
                        </xs:sequence>  
                        <xs:attribute name="genre" type="xs:string" use="required" />  
                        <xs:attribute name="publicationdate" type="xs:date" use="required" />  
                        <xs:attribute name="ISBN" type="xs:string" use="required" />  
                    </xs:complexType>  
                </xs:element>  
            </xs:sequence>  
        </xs:complexType>  
    </xs:element>  
</xs:schema>  

備註

屬性 ValueType 會取得目前節點之具型別值的 .NET Type

例如,類型的 xs:int 專案預設會是 ValueTypeInt32 。 不過, ValueType 可以是任何一個可以對應至 xs:int 的有效型別,例如 Int16Double

如果節點沒有類型,這相當於 節點上的類型批註。 xdt:untypedAtomic 在此情況下, ValueType 屬性會傳回節點的字串。 如需詳細資訊,請參閱 System.Xml 類別中的類型支援

適用於