Search code examples
c#xmlvalidationxsddocbook

Validation of Docbook 5.0 XML via XSD with C# .Net


I try to validate a docbook 5.0 xml via c# and xsd data.

I started from the example here: http://msdn.microsoft.com/en-us/library/ckztbtx6(v=vs.110).aspx where it is pretty easy and simple. I made the notValidXSD.xml (in the example) valid removing the first book calling it nowValidXSD.xml and it words fine.

Now I tried the same for the Docbook 5.0 format ( docbook.xsd, xml.xsd xlink.xml ) which you can download here http://www.docbook.org/xml/5.0/xsd/

Trying the code (given by the example on msdn above) gives me:

Validating XML file mydocxml.xml
Validation error: the http ://www.w3.org/XML/1998/namespace:id-Attribute is not declared.
Validation error: the http ://docbook.org/ns/docbook:article-Element  is not declared.
Validation error: the schema informationen für das Attribute 'version' could not be found.
Validation error: the http://docbook.org/ns/docbook:title-Element is not declared.
Validation error: the  http://docbook.org/ns/docbook:para-Element is not declared.
Validation finished. Validation failed.

(I had to make spaces after http because its my first question here) I don't know what to do any more. For me the files look fine. I tried Googling the problem for hours now and can't seem to be able to validate via c#.

Here are the files:

<!-- nowValidXSD.xml -->
<?xml version='1.0' encoding="UTF-8"?> 
<bookstore xmlns="urn:bookstore-schema"
     xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="urn:bookstore-schema books.xsd">
  <book genre="novel">
    <title>The Confidence Man</title>
    <author>
      <first-name>Herman</first-name>
      <last-name>Melville</last-name>
    </author>
    <price>11.99</price>
  </book>
</bookstore>

And the docbook xml:

<!--  mydocxml.xml -->
<?xml version="1.0"  encoding="UTF-8"?>   <!--   -->

<article xmlns="http ://docbook.org/ns/docbook" version="5.0"
   xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http ://docbook.org/ns/docbook docbook.xsd">
  <title>some title</title>
  <para> some text.</para>
</article>

Solution

  • The problem was that XmlValidatingReader is not only obsolete it is also not doing what is is supposed to be doing.

    I post my new Validation method basically from here: http://msdn.microsoft.com/en-US/library/as3tta56(v=vs.80).aspx in hopes that is helps someone :)

    private Boolean m_success = true;
    private void ValidationCallBack(object sender, ValidationEventArgs args)
    {
        m_success = false;
        Console.WriteLine("\r\n\tValidation error: " + args.Message);
    }
    
    
    
    public void validate(string xmlfile, string ns, string xsdfile)
    {
    
        m_success = true;
        XmlReaderSettings settings = new XmlReaderSettings();
        XmlSchemaSet sc = new XmlSchemaSet();
        sc.Add(ns, xsdfile);
        settings.ValidationType = ValidationType.Schema;
        settings.Schemas = sc;
        settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
    
        XmlReader reader = XmlReader.Create(xmlfile, settings);
        // Parse the file. 
        while (reader.Read()) ;
        Console.WriteLine("Validation finished. Validation {0}", (m_success == true ? "successful!" : "failed."));
        reader.Close();
    }