Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9184835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:08:59+00:00 2026-06-17T19:08:59+00:00

I am using C# with Mono, and I want to navigate a XML Schema

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T19:09:01+00:00Added an answer on June 17, 2026 at 7:09 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Mono.Cecil I want to rewrite the following property: public string FirstName { get
I'm trying to get a global hotkey working in Linux using Mono. I found
I'm trying to get the local IP address of my Android device using Mono
I want to start using Behaviour Driven Development (BDD) in Mono/MonoDevelop using a natural
Hi after refering to http://www.mono-project.com/Embedding_Mono i can call methods from managed code by using
I'm using Mono/MonoDevelop on Mac OS, and I want an example for using SqlMetal
I can't figure how to add custom attribute to a method using Mono.Cecil The
I am using mono 2.10.4 and monobjc. I want to be able to play
I want to run my c# application with OS Linux using Mono. I am
How do I start using Mono in Linux as a beginner when I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.