2

There is a XML schema file "books.xsd" as follows:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.contoso.com/books" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="bookstore" />
</xs:schema>

There is a XML file "books1.xml" as follows:

<?xml version="1.0" encoding="utf-8"?>
<bookstore xmlns="http://www.contoso.com/books">
</bookstore>

There is a XML file "books2.xml" as follows:

<?xml version="1.0" encoding="utf-8"?>
<bookstoreXXX xmlns="http://www.contoso.com/books">
</bookstoreXXX>

There is a XML file "books3.xml" as follows:

<?xml version="1.0" encoding="utf-8"?>
<bookstoreXXX xmlns="http://www.contoso.com/booksXXX">
</bookstoreXXX>

And I use these C# codes to test:

class Program
{
    public static void Main()
    {
        XmlDocument doc = new XmlDocument();
        doc.Load("booksN.xml");
        doc.Schemas.Add("http://www.contoso.com/books", "books.xsd");
        doc.Validate(booksSettingsValidationEventHandler);

        Console.WriteLine("\r\nPress ENTER to exit.");
        Console.Read();
    }

    static void booksSettingsValidationEventHandler(object sender, ValidationEventArgs e)
    {
        if (e.Severity == XmlSeverityType.Warning)
        {
            Console.Write("WARNING: ");
            Console.WriteLine(e.Message);
        }
        else if (e.Severity == XmlSeverityType.Error)
        {
            Console.Write("ERROR: ");
            Console.WriteLine(e.Message);
        }
    }
}

No error when validate "books1.xml".

An error "The 'http://www.contoso.com/books:bookstoreXXX' element is not declared." when use validate "books2.xml".

No error when validate "books3.xml".

But I want to get an error when validate "books3.xml", such as "The 'booksstoreXXX' element is not declared." or "The ''http://www.contoso.com/booksXXX' namespace is not declared.".

How can I modify the XML schema file or the C# codes?

ekad
  • 14,436
  • 26
  • 44
  • 46
Binky Bin
  • 21
  • 1
  • possible duplicate of [Validate xml against xsd without considering xml namesapce](http://stackoverflow.com/questions/30997892/validate-xml-against-xsd-without-considering-xml-namesapce) – Charles Mager Jul 19 '15 at 17:01

0 Answers0