Bug 9541

Summary: XPathNavigator.SchemaInfo is null after navigating XmlDocument
Product: [Mono] Class Libraries Reporter: dave_random+bugzilla
Component: System.XMLAssignee: Bugzilla <bugzilla>
Status: RESOLVED FIXED    
Severity: major CC: andrew.way, atsushi, bart.verthe, mono-bugs+mono, ramc
Priority: High    
Version: 2.10.x   
Target Milestone: Untriaged   
Hardware: PC   
OS: All   
Tags: Is this bug a regression?: ---
Last known good build:
Attachments: Test code to evaluate problem
Test XML Schema
Test XML

Description dave_random+bugzilla 2013-01-14 07:38:02 UTC
Created attachment 3208 [details]
Test code to evaluate problem

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. Some test code is attached, you can use the following XML and XSD files.

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

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

The same code run on Windows .Net (not Mono on Windows) 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
Comment 1 dave_random+bugzilla 2013-01-14 07:38:54 UTC
Created attachment 3209 [details]
Test XML Schema
Comment 2 dave_random+bugzilla 2013-01-14 07:39:18 UTC
Created attachment 3210 [details]
Test XML
Comment 3 Bart Verthé 2013-02-14 16:54:20 UTC
Ran also into this bug on debian wheezy 686, with: 
- mono 2.10.8.1 (deb package)
- mono 3.0.3 (own build from tarball on http://download.mono-project.com/sources/mono/mono-3.0.3.tar.bz2)

At first glance, XPathNavigator isn't the root cause.
In:
/tmp/buildd/mono-3.0.3/mcs/class/System.XML/System.Xml/XmlDocument.cs
...
886|case XmlNodeType.Element:
887|	XmlElement element = CreateElement(reader.Prefix, reader.LocalName, reader.NamespaceURI, reader.NameTable == this.NameTable);
888|#if NET_2_0
889|	if (reader.SchemaInfo != null)
890|			SchemaInfo = reader.SchemaInfo;
891|#endif
892|	element.IsEmpty = reader.IsEmptyElement;
...

Line 890 sets the property SchemaInfo for the XmlDocument everytime,for each node read. Resulting in not only the XmlDocument having the wrong SchemaInfo, but also none of the containing nodes having SchemaInfo.
While (at least I think, I'm not really familiar with the mono code base) it should set the SchemaInfo property for each created element.
Changing line 890 to: 
...
890|			element.SchemaInfo = reader.SchemaInfo;
...
Seems to fix described bug. 

Alas, loading XML's with a XSD still misses some (apparantly, but surprisingly less used?) functionality compared to .NET:
eg. .NET creates child text-nodes for empty elements in a XML-document when a default value is given in the XML-Schema, mono does no such thing.
Comment 4 Atsushi Eno 2014-04-23 23:28:13 UTC
Bart: applied your suggested change. Thanks.

For any unrelated bug, please file a new one (hopefully with a reproducible code).

[master 3e6f644]
Comment 5 Ram Chandra 2014-07-08 08:39:42 UTC
I have checked this issue on following builds

Mac OS X 10.9.3
Xamarin Studio : 5.2 (build 364)

Build Information
Release ID: 502000364
Git revision: fd8641a35ed89a183d04290f046a3aab5b09a867
Build date: 2014-07-07 13:16:52-04
Xamarin addins: 68026ad1ccee9923e927d9cfcca408d673d5ab61

To verify this issue I have created a console application and implemented the code mentioned in the bug description and I observed that after calling the XPathNavigator.MoveToFirstChild() the values of  XPathNavigator.SchemaInfo is not null.

Screencast: http://www.screencast.com/t/EZh3XP3QGU

Please let me know if this is sufficient to verify this issue or I have to check something else ?