如何在DNX Core 5.0(ASP.NET 5)中针对XSD(单独的文档)验证XML?

Nig*_*888 7 validation xsd asp.net-core-mvc dnx asp.net-core

我将一些代码移植到ASP.NET 5,并希望定位DNX Core 5.0.但是,我无法根据XSD文档查找验证XML文档所需的类型.

这是代码:

var xsdStream = this.GetType().GetTypeInfo().Assembly.GetManifestResourceStream(xsdPath);
using (XmlReader xsd = XmlReader.Create(xsdStream))
{
    XmlSchemaSet schema = new XmlSchemaSet();
    schema.Add(null, xsd);

    XmlReaderSettings xmlReaderSettings = new XmlReaderSettings();

    xmlReaderSettings.ValidationType = ValidationType.Schema;
    xmlReaderSettings.Schemas.Add(schema);

    using (XmlReader xmlReader = XmlReader.Create(xmlPath, xmlReaderSettings))
    {
        try
        {
            while (xmlReader.Read());
        }
        catch (Exception ex)
        {
            throw new Exception(string.Format(Resources.Messages.XmlValidationFailed, xmlPath), ex);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,我想要的就是停止第一个错误并抛出异常来指示错误是什么.

问题是:

  1. XmlSchemaSet类不存在的System.Xml.Schema命名空间(或其他任何地方我发现).
  2. XmlReaderSettings.ValidationTypeXmlReaderSettings.Schemas不存在性.

我检查了MSDN文档,它的方法略有不同.但是,以前XmlSchemaSet不存在,也没有XDocument.Validate().我还搜索了几个ASP.NET项目的示例,但似乎找不到任何.

DNX Core 5.0中存在哪些工具(如果有)来针对XSD验证XML?如果可能的话,我宁愿使用流来做这件事,但如果绝对必要,我会接受一种方法,将整个文档一次性读入内存.

Tho*_*mas 1

第一个版本不支持 XSD。当我在他们发布的推文、帖子、错误或社区站立会议中听到正确的消息时,就会考虑稍后发布。

PS:Pawel 应该回答这个问题并获得学分……但我们应该结束这个问题。