private Boolean validate(IController controller, XmlDocument document) { XmlNodeReader nodeReader = new XmlNodeReader(document); Boolean isValid = false; XmlReader schemaDatabase = controller.GetXSD("database.xsd"); XmlReaderSettings settings = new XmlReaderSettings(); settings.ValidationType = ValidationType.Schema; settings.Schemas.Add(null, schemaDatabase); //settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema; //settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation; settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings; settings.ValidationEventHandler += new ValidationEventHandler(validationEventHandler); XmlReader reader = XmlReader.Create(nodeReader, settings); try { while (reader.Read()) ; isValid = true; } catch (System.Exception e) { System.Windows.Forms.MessageBox.Show(e.Message, "Validation Error"); } finally { schemaDatabase.Close(); nodeReader.Close(); reader.Close(); } return isValid; }
protected XPathNavigator validate(IController validatingController, XmlDocument document, String xsdPath) { XPathNavigator navValidatedXml = null; XmlReader schemaDatabase = validatingController.GetXSD(xsdPath); XmlReaderSettings settings = new XmlReaderSettings(); settings.ValidationType = ValidationType.Schema; settings.Schemas.Add(null, schemaDatabase); settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings; settings.ValidationEventHandler += new ValidationEventHandler(validationEventHandler); StringReader strReader = new StringReader(document.OuterXml); XmlReader xmlReader = XmlReader.Create(strReader, settings); XmlDocument doc = new XmlDocument(); try { doc.Load(xmlReader); navValidatedXml = doc.CreateNavigator(); } catch (System.Exception e) { System.Windows.Forms.MessageBox.Show(e.Message, "Validation Error"); } finally { schemaDatabase.Close(); strReader.Close(); xmlReader.Close(); } return navValidatedXml; }