views:

548

answers:

1

Hi all,

Im trying to validate an XML file using a .DTD but it gives me the following error.

'ENTITY' is an unexpected token. The expected token is 'DOCTYPE'. Line 538, position 3.

        public static void Validate(string xmlFilename, string schemaFilename)
    {
        XmlTextReader r = new XmlTextReader(xmlFilename);
        XmlValidatingReader validator = new XmlValidatingReader(r);
        validator.ValidationType = ValidationType.Schema;

        XmlSchemaCollection schemas = new XmlSchemaCollection();
        schemas.Add(null, schemaFilename);


        validator.ValidationEventHandler += new ValidationEventHandler(ValidationEventHandler);

        try
        {
            while (validator.Read())
            { }
        }
        catch (XmlException err)
        {
            Console.WriteLine(err.Message);
        }
        finally
        {
            validator.Close();
        }
    }

The DTD im using to validate = http://www.editeur.org/onix/2.1/reference/onix-international.dtd

I hope someone can help me thanks!

A: 

Edit:

Just noticed: your validation type is also set wrong. Try setting it to ValidationType.DTD instead of Schema.

ValidationType at MSDN

--

The error means exactly as it states- the DTD that is referenced is not well formed, as DOCTYPE should be present before any other declarations in a DTD.

Document Type Definition (Wikipedia)

Introduction to DTD (w3schools)

You might be able to get around this by downloading a local copy, modifying it to add in the expected root element yourself, and then referencing your edited version in your source.

meklarian