public void Validate(string xmlPath, ValidationErrorDelegate validationErrorDelegate) { XmlUrlResolver xmlResolver = new XmlUrlResolver(); xmlResolver.Credentials = System.Net.CredentialCache.DefaultCredentials; XmlReaderSettings rs = new XmlReaderSettings(); rs.ValidationType = ValidationType.Schema; rs.IgnoreComments = true; rs.IgnoreWhitespace = true; rs.IgnoreProcessingInstructions = true; rs.XmlResolver = xmlResolver; List <string> errors = new List <string>(); _groupedErrors = null; using (XmlTextReader textReader = new XmlTextReader(xmlPath)) { rs.ValidationEventHandler += delegate(object sender, ValidationEventArgs args) { AppendError(textReader, args, validationErrorDelegate); }; using (XmlReader reader = XmlReader.Create(textReader, rs)) { while (reader.Read()) /*Empty loop*/ } { } } OutputGroupedErrors(validationErrorDelegate); }
public void Validate(Stream xmlStream, Stream xsdStream, ValidationErrorDelegate validationErrorDelegate) { _groupedErrors = null; XmlSchema schema = new XmlSchema(); using (StreamReader schemaReader = new StreamReader(xsdStream)) { schema = XmlSchema.Read(schemaReader, delegate(object sender, ValidationEventArgs args) { AppendError(args, validationErrorDelegate); }); } XmlReaderSettings readerSettings = new XmlReaderSettings(); readerSettings.ValidationType = ValidationType.Schema; readerSettings.Schemas.Add(schema); using (XmlTextReader textReader = new XmlTextReader(xmlStream)) { readerSettings.ValidationEventHandler += delegate(object sender, ValidationEventArgs args) { AppendError(textReader, args, validationErrorDelegate); }; using (XmlReader reader = XmlReader.Create(textReader, readerSettings)) { while (reader.Read()) /*Empty loop*/ } { } } OutputGroupedErrors(validationErrorDelegate); }
public void Validate(byte[] xmlContent, Stream xsdStream, ValidationErrorDelegate validationErrorDelegate) { using (Stream stream = new MemoryStream(xmlContent, false)) { Validate(stream, xsdStream, validationErrorDelegate); } }
public void Validate(string xmlPath, Stream xsdStream, ValidationErrorDelegate validationErrorDelegate) { using (Stream stream = File.OpenRead(xmlPath)) { Validate(stream, xsdStream, validationErrorDelegate); } }
private void AppendError(ValidationEventArgs args, ValidationErrorDelegate validationErrorDelegate) { if (GroupErrors) { AppendGroupedError(args.Message); } else { validationErrorDelegate(args.Message); } }
private void AppendError(XmlTextReader reader, ValidationEventArgs args, ValidationErrorDelegate validationErrorDelegate) { if (GroupErrors) { AppendGroupedError(args.Message); } else { validationErrorDelegate(string.Format("Line: {0} - Position: {1} - {2}", reader.LineNumber, reader.LinePosition, args.Message)); } }
private void OutputGroupedErrors(ValidationErrorDelegate validationErrorDelegate) { if (GroupErrors) { if (_groupedErrors != null) { foreach (KeyValuePair <string, int> pair in _groupedErrors) { string message = string.Format("{0} {1}{2}{3}", pair.Value.ToString(), (pair.Value == 1) ? "occurrences:" : "occurrences:", Environment.NewLine, pair.Key); validationErrorDelegate(message); } } } }
public void Validate(string xmlPath, string xsdPath, ValidationErrorDelegate validationErrorDelegate) { using (Stream xmlStream = File.OpenRead(xmlPath)) { using (Stream xsdStream = File.OpenRead(xsdPath)) { string startDirectory = Environment.CurrentDirectory; Environment.CurrentDirectory = Path.GetDirectoryName(xsdPath); try { Validate(xmlStream, xsdStream, validationErrorDelegate); } finally { Environment.CurrentDirectory = startDirectory; } } } }