Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Problem with XML Validation against schema

From: "espresso" <carsten.sponsel@---.-->
To: NULL
Date: 12/6/2006 10:47:00 AM

Hello,

I've written a method that checks it a XML file conforms to a specific
XML schema. One of my XML schemas is rather complex and involves
namespaces etc.

Now I'm having the problem, that if I use my method with any general
XML file, that does not even has the right root element, it returns
true, which means the xml file conforms to that xsd. Bad problem :-(
When i check the XML file with XMLSpy (i have to assign the schema
first), XMLSpy says the XML file is not valid - of course.

If I use my method with a XML file, that has the right root element,
but some small error, my method complains that the XML file is not
valid - which is the right behaviour.

Valid XML files are correctly recognized as valid.

I suppose that has something to do with XML namespaces or with the
complex schema itself, because my method works well with less complex
schemas that do not contain namespaces.

So my question is, if someone can imagine what problem that could be (a
general problem in the schema, or incorrect handling of xml namespaces
in my code?). I do not want to burden you with my schema file - because
it's rather complex and huge.

Here's my method:

public static bool isXmlFileValid (string sXmlFilename, string
sXProtSchema)
        {
            Common.Reject.ifNull (sXmlFilename);
            Common.Reject.ifNull (sXProtSchema);
            bool bStatus = true;
            XmlDocument xmlDoc = new XmlDocument ();
            XmlReader xmlReader = null;
            XmlReaderSettings xmlReaderSettings = new XmlReaderSettings
();

            try
            {
                xmlReaderSettings.Schemas.Add (null, sXProtSchema);
            }
            catch (ApplicationException)
            {
                return false;
            }

            xmlReaderSettings.ValidationType = ValidationType.Schema;
            xmlReaderSettings.ConformanceLevel =
ConformanceLevel.Document;

            try
            {
                xmlReader = XmlReader.Create (sXmlFilename,
xmlReaderSettings);

                if (xmlReader != null)
                {
                    if (xmlDoc != null)
                    {
                        xmlDoc.Load (xmlReader);
                    }
                    else
                    {
                        bStatus = false;
                    }
                }
                else
                {
                    bStatus = false;
                }
            }
            catch (System.InvalidOperationException)
            {
                bStatus = false;
            }
            catch (System.Xml.Schema.XmlSchemaValidationException ex)
            {
                bStatus = false;
            }
            catch (System.Xml.XmlException)
            {
                bStatus = false;
            }
            catch (System.Exception e)
            {
                System.Console.WriteLine (e.GetType ().ToString ());
                System.Console.WriteLine (e.Message + ": " +
e.StackTrace);
                throw;
            }
            finally
            {
                if (xmlReader != null)
                {
                    xmlDoc = null;
                    xmlReader.Close ();
                    xmlReader = null;
                }
            }
            return bStatus;
        }

Thank you very much for your time.

Carsten



transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent