XPathNavigator.ValueType プロパティ
定義
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。The .NET Type of the current node. 既定値は String です。The default value is String.
例
次の例では、 contosoBooks.xml
xml ファイルと contosoBooks.xsd
xml スキーマ定義言語 (XSD) スキーマを使用してオブジェクトを作成し XPathNavigator ます。In the following example, the contosoBooks.xml
XML file and contosoBooks.xsd
XML Schema definition language (XSD) schema are used to create an XPathNavigator object. 要素の型指定された値 price
は、プロパティを使用して表示され、 ValueType メソッドを使用して文字列として返され ValueAs ます。The typed value of the price
element is displayed using the ValueType property and then returned as a string using the ValueAs method.
// 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
ファイルを入力として使用します。The example takes the contosoBooks.xml
file as an input.
<?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
ファイルも入力として使用します。The example also takes the contosoBooks.xsd
as an input.
<?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 ます。The ValueType property gets the .NET Type of the typed value of the current node.
たとえば、型の要素には、 xs:int
ValueType 既定でのが含ま Int32 れます。For example, an element of type xs:int
would have a ValueType of Int32 by default. ただし、 ValueType には、やなど、にマップできる有効な型のいずれかを指定でき xs:int
Int16 Double ます。However, the ValueType could be any one of the valid types that can be mapped to xs:int
such as Int16 or Double.
ノードに型がない場合、これはノード上の型の注釈に相当 xdt:untypedAtomic
します。If a node has no type, this is equivalent to the type annotation on the node being xdt:untypedAtomic
. この場合、プロパティは ValueType ノードの文字列を返します。In this case, the ValueType property returns a string for the node. 詳細については、「 System.Xml クラスでの型のサポート」を参照してください。For more information, see Type Support in the System.Xml Classes.