XPathNavigator.ValueType Property


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Gets the .NET Framework Type of the current node.

Namespace:   System.Xml.XPath
Assembly:  System.Xml (in System.Xml.dll)

public override Type ValueType { get; }

Property Value

Type: System.Type

The .NET Framework Type of the current node. The default value is String.

The ValueType property gets the .NET Framework Type of the typed value of the current node.

For example, an element of type xs:int would have a ValueType of Int32 by default. However, the ValueType could be any one of the valid types that can be mapped to xs:int such as Int16 or Double.

If a node has no type, this is equivalent to the type annotation on the node being xdt:untypedAtomic. In this case, the ValueType property returns a string for the node. For more information, see Type Support in the System.Xml Classes.

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. 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 = 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.

// Get the value of the price element as a string and display it.
string price = navigator.ValueAs(typeof(string)) as string;

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>
    <book genre="novel" publicationdate="1967-11-17" ISBN="0-201-63361-2">
        <title>The Confidence Man</title>
    <book genre="philosophy" publicationdate="1991-02-15" ISBN="1-861001-57-6">
        <title>The Gorgias</title>

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:element maxOccurs="unbounded" name="book">
                            <xs:element name="title" type="xs:string" />
                            <xs:element name="author">
                                        <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:element name="price" type="xs:decimal" />
                        <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" />

.NET Framework
Available since 2.0
Return to top