canihavesomecoffee
canihavesomecoffee

Reputation: 163

XML validation with XSD fails while online validators pass

I have a simple XML file that I want to validate against an XSD. However, whenever I run my validation code, I get a validation exception saying the "multitest" node is undefined (unlocalized error message: "The multitest element is not declared."). The strange thing is that I tried to validate the XML against the XSD on a couple of online validation sites (http://www.xmlvalidation.com, http://www.freeformatter.com/xml-validator-xsd.html), and they say the XML is perfectly valid.

This is the code I use to validate (C# with the Mono library)

// Resources.tests contains the XSD file in string format, xmlFileName points to the XML file location
using (StringReader sr = new StringReader (Resources.tests)) {
    XmlReader r = XmlReader.Create (sr);
    XmlReaderSettings settings = new XmlReaderSettings ();
    settings.Schemas.Add (null, r);
    settings.ValidationType = ValidationType.Schema;
    using (FileStream fs = new FileStream (xmlFileName, FileMode.Open)) {
        var reader = XmlReader.Create (fs, settings);
        while (reader.Read ()) {
            // Nothing in here, just need to read out the entire file in a loop.
        }
    }
}

This is the XML file I try to validate:

<?xml version="1.0" encoding="UTF-8"?>
<multitest>
    <testfile>
        <location>blah.xml</location>
    </testfile>
</multitest>

The XSD is quite simple as well:

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="multitest">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="testfile" maxOccurs="unbounded" minOccurs="1">
          <xs:complexType>
            <xs:sequence>
              <xs:element type="xs:string" name="location"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

What am I missing?

Addition:

Strangely, the same code works perfectly on the XML and XSD given below:

XSD:

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="tests">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="test" maxOccurs="unbounded" minOccurs="1">
          <xs:complexType>
            <xs:sequence>
              <xs:element type="xs:string" name="sample"/>
              <xs:element type="xs:string" name="cmd"/>
              <xs:element type="xs:string" name="result"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

XML:

<?xml version="1.0" encoding="UTF-8"?>
<tests>
  <test>
    <sample>blahblah.txt</sample>
    <cmd>samplecmd</cmd>
    <result>blahblahblah_result.txt</result>
  </test>
</tests>

Upvotes: 2

Views: 459

Answers (1)

Charles Mager
Charles Mager

Reputation: 26223

I would vote this is closed as it's essentially a typo rather than an actual problem.

For completeness, I'll point out that you're validating against the wrong schema. Your first line that reads your schema string is this:

using (StringReader sr = new StringReader (Resources.tests))

And Resources.tests sounds like it refers to the second schema in your question (and you have confirmed this). Simply change this to the correct schema and it should resolve your issue.

Upvotes: 0

Related Questions