• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 237
  • Last Modified:

Why cannont validate the xml fragment?

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:bookstore-schema" elementFormDefault="qualified" targetNamespace="urn:bookstore-schema">
      <xsd:element name="bookstore" type="bookstoreType" />
      <xsd:element name="comment" type="xsd:string" />
      <xsd:element name="author" type="authorName"/>
      <xsd:complexType name="authorName">
                  <xsd:element name="first-name" type="xsd:string" />
                  <xsd:element name="last-name" type="xsd:string" />
      <xsd:complexType name="bookstoreType">
            <xsd:sequence maxOccurs="unbounded">
                  <xsd:element name="book" type="bookType" />
                  <xsd:element ref="comment" minOccurs="0" />
      <xsd:complexType name="bookType">
                  <xsd:element name="title" type="xsd:string" />
                  <xsd:element ref="author" />
                  <xsd:element name="price" type="xsd:decimal" />
            <xsd:attribute name="genre" type="xsd:string" />


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

namespace FragmentValidation {
      /// <summary>
      /// Summary description for Class1.
      /// </summary>
      class Class1 {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            System.Boolean m_success;

            static void Main(string[] args) {
                  // TODO: Add code to start application here.
                  XmlValidatingReader reader  = null;
                  XmlSchemaCollection myschema = new XmlSchemaCollection();
                  ValidationEventHandler eventHandler = new ValidationEventHandler(Class1.ShowCompileErrors );

                  try {
                        //Create the XML fragment to be parsed.
                        //String xmlFrag = "<author  xmlns='urn:bookstore-schema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>" +
                        //      "<first-name>Herman</first-name>" +
                        //      "<last-name>Melville</last-name>" +
                        //      "</author>";

                        String xmlFrag = "<first-name xmlns='urn:bookstore-schema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>Herman</first-name>";

                        //Create the XmlParserContext.
                        XmlParserContext context = new XmlParserContext(null, null, "", XmlSpace.None);

                        //Implement the reader.
                        reader = new XmlValidatingReader(xmlFrag, XmlNodeType.Element, context);
                        //Add the schema.
                        myschema.Add("urn:bookstore-schema", "c:\\Books.xsd");

                        //Set the schema type and add the schema to the reader.
                        reader.ValidationType = ValidationType.Schema;

                        while (reader.Read()) {

                        Console.WriteLine("Completed validating xmlfragment");
                  catch (XmlException XmlExp) {
                  catch(XmlSchemaException XmlSchExp) {
                  catch(Exception GenExp) {
                  finally {

            public static void ShowCompileErrors(object sender, ValidationEventArgs args) {
                  Console.WriteLine("Validation Error: {0}", args.Message);

Why the local element first-name cannot be validated:
undeclared "urn:bookstore-schema:first-name" element ?

How to handler it? thanks
1 Solution
Geert BormansCommented:
Hi zhghl,
>      <xsd:element name="author" type="authorName"/>
>      <xsd:complexType name="authorName">
>           <xsd:sequence>
>                <xsd:element name="first-name" type="xsd:string" />
>                <xsd:element name="last-name" type="xsd:string" />
>           </xsd:sequence>
>      </xsd:complexType>


>String xmlFrag = "<first-name xmlns='urn:bookstore-schema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>Herman</first-name>";

you defined first-name as a local element inside type authorName
that means it simply doesn't exist outside that scope
if you want to use first-name as a root element of your XML, as in xmlFrag
you need to define it globally
like this

    <xsd:complexType name="authorName">
            <xsd:element ref="first-name"/>
            <xsd:element name="last-name" type="xsd:string" />
    <xsd:element name="first-name"  type="xsd:string" />

your original test fragment
                    //String xmlFrag = "<author  xmlns='urn:bookstore-schema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>" +
                    //     "<first-name>Herman</first-name>" +
                    //     "<last-name>Melville</last-name>" +
                    //     "</author>";

is valid with both schema's

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now