I have a c# script that validates an XML document against an XSD document, as follows:
static bool IsValidXml(string xmlFilePath, string xsdFilePath) { XmlReaderSettings settings = new XmlReaderSettings(); settings.Schemas.Add(null, xsdFilePath); settings.ValidationType = ValidationType.Schema; settings.Schemas.Compile(); try { XmlReader xmlRead = XmlReader.Create(xmlFilePath, settings); while (xmlRead.Read()) { }; xmlRead.Close(); } catch (Exception e) { return false; } return true; }
I've compiled this after looking at a number of MSDN articles and questions here where this is the solution. It does correctly validate that the XSD is formed well (returns false if I mess with the file) and checks that the XML is formed well (also returns false when messed with).
I've also tried the following, but it does the exact same thing:
static bool IsValidXml(string xmlFilePath, string xsdFilePath) { XDocument xdoc = XDocument.Load(xmlFilePath); XmlSchemaSet schemas = new XmlSchemaSet(); schemas.Add(null, xsdFilePath); try { xdoc.Validate(schemas, null); } catch (XmlSchemaValidationException e) { return false; } return true; }
I've even pulled a completely random XSD off the internet and thrown it into both scripts, and it still validates on both. What am I missing here?
Using .NET 3.5 within an SSIS job.
No comments:
Post a Comment