Altova Mailing List Archives>Archive Index >microsoft.public.xml Archive Home >Recent entries [Thread Prev] >Thread Next - Re: Problem with XML Validation against schema Problem with XML Validation against schemaTo: 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 | ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
