Question

Its the weird-weird situation once again :)

I've function that verifies given XML against XSD and throws exception when the validation fails. (the function is called from WebPage that receives the xml calls)

both the call

if (!xmlvld.ValidXmlDoc(X, "", "https://somepathtofile.xsd")) 
    throw new Exception(xmlvld.ValidationError + "1");

and the validating function are pretty simple

   /// <summary>
    /// This method validates an xml string against an xml schema.
    /// </summary>
    /// <param name="xml">StringReader containing xml</param>
    /// <param name="schemaNamespace">XML Schema Namespace</param>
    /// <param name="schemaUri">XML Schema Uri</param>
    /// <returns>bool</returns>
    public bool ValidXmlDoc(StringReader xml, string schemaNamespace, string schemaUri)
    {
        // Continue?
        if (xml == null || schemaNamespace == null || schemaUri == null)
        {
            return false;
        }

        isValidXml = true;
        XmlValidatingReader vr;
        XmlTextReader tr;
        XmlSchemaCollection schemaCol = new XmlSchemaCollection();
        schemaCol.Add(schemaNamespace, schemaUri);

        try
        {
            // Read the xml.
            tr = new XmlTextReader(xml);
            // Create the validator.
            vr = new XmlValidatingReader(tr);
            // Set the validation tyep.
            vr.ValidationType = ValidationType.Auto;
            // Add the schema.
            if (schemaCol != null)
            {
                vr.Schemas.Add(schemaCol);
            }
            // Set the validation event handler.
            vr.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
            // Read the xml schema.
            while (vr.Read())
            {
            }

            vr.Close();

            return isValidXml;
        }
        catch (Exception ex)
        {
            this.ValidationError = ex.Message;
            return false;
        }
        finally
        {
            // Clean up...
            vr = null;
            tr = null;
        }
    }

And this worked very well for the last 2 years, but recently the validation function started to return the follwing message:

[ValidationError]Circular attribute group reference.[/ValidationError]

Which is false message - neither the XSD nor the sent XML have changed, furthermore if I recycle the Application Pool, and send the same XML again (I've "ping" program that sends the same XML every few seconds and checks the result) the validation passes... for a while (some is between few minutes and few hours).

The XSD is sitting in the same directory as the calling page and in the server log I see the request to the XSD with HTTP/200 response, I even tried changeing the HTTPS to local path but the error remained the same (pointing to invalid address gives HTTP/404 or invalid path exceptions so it does attempt to read the XSD file)

Again - recycling the apppool resolves it immediately but I can't recycle every 5 mins.

Thanks in advance! Simon

Was it helpful?

Solution

You won't like this answer, but apparently the XmlValidatingReader was obsoleted in .NET 2.0... perhaps because of issues like this?

The suggestion is to validate using an XmlReader. That link is for the 2.0 version of the help files, which includes some details specific to converting from an XmlValidatingReader; more recent versions of the help are available from the drop-down at the top.

This link gives details about the XmlValidatingReader being obsolete.

An example of using an XmlReader is here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top