Validating an XML against referenced XSD in C#

后端 未结 5 662
天命终不由人
天命终不由人 2020-11-22 13:33

I have an XML file with a specified schema location such as this:

xsi:schemaLocation=\"someurl ..\\localSchemaPath.xsd\"

I want to validate

5条回答
  •  春和景丽
    2020-11-22 14:26

    personally I favor validating without a callback:

    public bool ValidateSchema(string xmlPath, string xsdPath)
    {
        XmlDocument xml = new XmlDocument();
        xml.Load(xmlPath);
    
        xml.Schemas.Add(null, xsdPath);
    
        try
        {
            xml.Validate(null);
        }
        catch (XmlSchemaValidationException)
        {
            return false;
        }
        return true;
    }
    

    (see Timiz0r's post in Synchronous XML Schema Validation? .NET 3.5)

提交回复
热议问题