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
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Geert BormansInformation ArchitectCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.