Move to a child with XPathNavigator : XmlPathNavigator « XML « C# / CSharp Tutorial






using System;
using System.Xml;
using System.Xml.Schema;
using System.Xml.XPath;

class ValidatingReaderExample
{
    static void Main(string[] args)
    {
        try
        {
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.Schemas.Add("http://www.yourname.com/books", "contosoBooks.xsd");
            settings.ValidationType = ValidationType.Schema;

            XmlReader reader = XmlReader.Create("contosoBooks.xml", settings);

            XmlDocument document = new XmlDocument();
            document.Load(reader);
            XPathNavigator navigator = document.CreateNavigator();

            ValidationEventHandler validation = new ValidationEventHandler(SchemaValidationHandler);

            navigator.MoveToChild("bookstore", "http://www.yourname.com/books");

            navigator.AppendChild("<title>Book Title</title>");
            
            document.Validate(validation);

            navigator.MoveToParent();
            navigator.MoveToChild("price", "http://www.yourname.com/books");
            navigator.SetTypedValue(DateTime.Now);
        }
        catch (Exception e)
        {
            Console.WriteLine("ValidatingReaderExample.Exception: {0}", e.Message);
        }
    }

    static void SchemaValidationHandler(object sender, ValidationEventArgs e)
    {
        switch (e.Severity)
        {
            case XmlSeverityType.Error:
                Console.WriteLine("Schema Validation Error: {0}", e.Message);
                break;
            case XmlSeverityType.Warning:
                Console.WriteLine("Schema Validation Warning: {0}", e.Message);
                break;
        }
    }
}








30.23.XmlPathNavigator
30.23.1.XPathNavigator: XPathNodeIterator with comparison
30.23.2.XPathNavigator: Evaluate
30.23.3.Create XPathNavigator object by calling CreateNavigator of XmlDocument
30.23.4.XPathNodeIterator from a select-node statement
30.23.5.Select a node
30.23.6.Get Node information by using XPathNavigator
30.23.7.Move to a child with XPathNavigator
30.23.8.Fast select by ID using XPathNavigator API