Why cannont validate the xml fragment?

Posted on 2006-04-21
Last Modified: 2013-11-19
<xsd:schema xmlns:xsd="" 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=''>" +
                        //      "<first-name>Herman</first-name>" +
                        //      "<last-name>Melville</last-name>" +
                        //      "</author>";

                        String xmlFrag = "<first-name xmlns='urn:bookstore-schema' xmlns:xsi=''>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
Question by:zhghl
    1 Comment
    LVL 60

    Accepted Solution

    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=''>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=''>" +
                        //     "<first-name>Herman</first-name>" +
                        //     "<last-name>Melville</last-name>" +
                        //     "</author>";

    is valid with both schema's

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Suggested Solutions

    Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
    Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now