我有一个具有指定模式位置的 XML 文件,如下所示:
xsi:schemaLocation="someurl ..\localSchemaPath.xsd"
我想在 C # 中验证。当我打开文件时,VisualStudio 根据架构验证它并完美地列出错误。然而,不知怎么的,我似乎不能在 C # 中自动验证它,除非指定要验证的模式,比如:
XmlDocument asset = new XmlDocument();
XmlTextReader schemaReader = new XmlTextReader("relativeSchemaPath");
XmlSchema schema = XmlSchema.Read(schemaReader, SchemaValidationHandler);
asset.Schemas.Add(schema);
asset.Load(filename);
asset.Validate(DocumentValidationHandler);
我是否应该能够使用 XML 文件中指定的模式自动进行验证?我错过了什么?