Валидация xml-файла по xsd-схеме

Проверка соответствия xml-файла xsd-схеме:

private bool ValidateXmlWithXsd(string xmlUri, string xsdUri, bool warnings = false)
{
    XmlSchema xmlSchema = XmlSchema.Read(XmlReader.Create(xsdUri), null);
    XmlReaderSettings xmlReaderSettings = new XmlReaderSettings();
    xmlReaderSettings.Schemas = new System.Xml.Schema.XmlSchemaSet();
    xmlReaderSettings.Schemas.Add(xmlSchema);
    xmlReaderSettings.ValidationType = ValidationType.Schema;
        if (warnings)
        {
            xmlReaderSettings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
        }
        try
        {
            XmlReader reader = XmlReader.Create(xmlUri, xmlReaderSettings);
            while (reader.Read()) ;
        }
        catch (XmlSchemaValidationException ex)
        {
                MessageBox.Show(ex.Message, this.Text);
                return false;
        }
        catch (XmlException ex)
        {
            MessageBox.Show(ex.Message, this.Text);
                return false;
        }
        return true;
}


Пример на github Скачать XMLValidator.zip 12,60 kB


C# XmlValidation