Пример #1
0
        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);
    }
Пример #2
0
    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);
}
Пример #3
0
public void Validate(byte[] xmlContent, Stream xsdStream,
                     ValidationErrorDelegate validationErrorDelegate)
{
    using (Stream stream = new MemoryStream(xmlContent, false))
    {
        Validate(stream, xsdStream, validationErrorDelegate);
    }
}
Пример #4
0
public void Validate(string xmlPath, Stream xsdStream,
                     ValidationErrorDelegate validationErrorDelegate)
{
    using (Stream stream = File.OpenRead(xmlPath))
    {
        Validate(stream, xsdStream, validationErrorDelegate);
    }
}
Пример #5
0
private void AppendError(ValidationEventArgs args, ValidationErrorDelegate validationErrorDelegate)
{
    if (GroupErrors)
    {
        AppendGroupedError(args.Message);
    }
    else
    {
        validationErrorDelegate(args.Message);
    }
}
Пример #6
0
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));
    }
}
Пример #7
0
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);
            }
        }
    }
}
Пример #8
0
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;
            }
        }
    }
}