Question

I am using C# with Mono, and I want to navigate a XML Schema validated XmlDocument using XPathNavigator. The point being as I traverse the document I can get the XML SChema information for each element via the XPathNavigator.SchemaInfo property. However after I call XPathNavigator.MoveToFirstChild() the XPathNavigator.SchemaInfo = null. Here is an example

using System;

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

namespace XmlSchemaTest
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlDocument xmlDocument = ReadAndValidateXmlFile(@"../test.xml", @"../test.xsd");

            if (xmlDocument == null)
                Console.WriteLine("Cannot open document or it didn't validate.");
            else
            {
                XPathNavigator xpathNavigator = xmlDocument.CreateNavigator();
                Console.WriteLine("XPathNavigator.SchemaInfo is " + ((xpathNavigator.SchemaInfo == null) ? "null" : "not null"));

                xpathNavigator.MoveToRoot();
                Console.WriteLine("Called XPathNavigator.MoveToRoot()");

                if(xpathNavigator.MoveToFirstChild())
                {
                    Console.WriteLine("XPathNavigator.LocalName after .MoveToFirstChild() succeeded = " + xpathNavigator.LocalName);
                    Console.WriteLine("XPathNavigator.NodeType value = " + xpathNavigator.NodeType.ToString());
                    Console.WriteLine("XPathNavigator.SchemaInfo is " + ((xpathNavigator.SchemaInfo == null) ? "null" : "not null"));
                }
            }

            //Console.ReadLine();
        }

        private static XmlDocument ReadAndValidateXmlFile(string xmlPath, string xsdPath)
        {
            // Load the XML Schema
            bool anyValidationErrors = false;
            XmlSchemaSet oSchemaSet = new XmlSchemaSet();
            ValidationEventHandler Handler = new ValidationEventHandler((object sender, ValidationEventArgs args) => {Console.WriteLine(args.Message); anyValidationErrors = true;} );
            oSchemaSet.ValidationEventHandler += Handler;
            XmlSchema oSchema = null;
            using (StreamReader sr = new StreamReader(xsdPath)) {
                oSchema = XmlSchema.Read(sr, Handler);  
            }
            if (anyValidationErrors || (oSchema == null)) {
                Console.WriteLine("Schema validation errors");
                return null;    
            }
            oSchemaSet.Add(oSchema);

            // Set up the Xml reader to do schema validation
            XmlReaderSettings xmlReaderSettings = new XmlReaderSettings();
            xmlReaderSettings.ValidationType = ValidationType.Schema;
            xmlReaderSettings.Schemas.Add(oSchemaSet);
            anyValidationErrors = false;
            xmlReaderSettings.ValidationEventHandler += new ValidationEventHandler((object sender, ValidationEventArgs args) => {Console.WriteLine(args.Message); anyValidationErrors = true;} );

            // Load the Xml and validate against schemer
            using (XmlReader xmlReader = XmlReader.Create(xmlPath, xmlReaderSettings))
            {
                XmlDocument xmlDocument = new XmlDocument();
                xmlDocument.Load(xmlReader);

                if (anyValidationErrors) {
                    Console.WriteLine("Xml validation errors");
                    return null;
                }
                else
                    return xmlDocument;
            }
        }
    }
}

With this XSD

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="TEST" 
            targetNamespace="urn:test" 
            xmlns:tst="urn:test" 
            xmlns="urn:test" 
            elementFormDefault="qualified"
            xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="TestElement">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="SubEle">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="ChildEle" type="xs:unsignedInt" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

And this XML

<?xml version="1.0" encoding="utf-8" ?>
<tst:TestElement xmlns:tst="urn:test">
  <tst:SubEle>
    <tst:ChildEle>123</tst:ChildEle>
  </tst:SubEle>
</tst:TestElement>

Gives this output

XPathNavigator.SchemaInfo is not null
Called XPathNavigator.MoveToRoot()
XPathNavigator.LocalName after .MoveToFirstChild() succeeded = TestElement
XPathNavigator.NodeType value = Element
XPathNavigator.SchemaInfo is null

Any one have suggestions as to what is going on or what I am doing wrong?

thanks Dave

PS I say "in mono" because that is what I am using, I haven't been able to confirm on Windows yet. Additionally, it's runtime version is .Net 4.0, happens in Debug and Release.

UPDATE I just tried this on Windows and got this result

XPathNavigator.SchemaInfo is not null
Called XPathNavigator.MoveToRoot()
XPathNavigator.LocalName after .MoveToFirstChild() succeeded = TestElement
XPathNavigator.NodeType value = Element
XPathNavigator.SchemaInfo is not null
Press any key to continue . . .

So maybe a Mono thing?

Was it helpful?

Solution

I ended up filing a bug - https://bugzilla.xamarin.com/show_bug.cgi?id=9541

I am working around the issue by traversing from the root XmlSchemaElement in the XmlSchemaSet and recording all XmlSchemaElements and XmlSchemaAttributes by XmlQualifiedName, and then when traversing the XmlDocument by XPathNavigator, using the XmlQualifiedName from the XPathNavigator to look up the XmlSchemaElement/XmlSchemaAttribute. It kinda works, but also doesn't because an XmlQualifiedName is not enough to uniquely identify an element isntance in the schema e.g. an element can be used in various places in a schema with different maxOccurs/minOccurs values.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top