本文介绍了用于 xml 架构编译的 Full Framework 和 .NET Core 的不同行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的验证码:

string xsdPath = "base.xsd";
XDocument doc = XDocument.Load(xmlPath);
XmlSchemaSet schemas = new XmlSchemaSet();
schemas.Add("http://some.domain.org", xsdPath);
schemas.Compile();
bool isValid = true;
doc.Validate(schemas, (o, e) => {
    res.AddMessage(MessageSeverities.Error, $"{e.Severity}:{e.Message}");
    isValid = false;
});
if ( isValid ) {
    res.AddMessage(
        MessageSeverities.Notice,
        $"{formFile.FileName} is valid!");
}

此代码在桌面应用 (.net 4.6) 中使用时运行良好

this code runs fine when used in a desktop app (.net 4.6)

在 .net core asp 2.1 控制器中使用时代码失败,并由 schemas.Compile(); 引发以下异常:

the code fails when used in a .net core asp 2.1 controller with the following exception raised by schemas.Compile();:

XmlSchemaException:未声明类型http://some.domain.org:tAccountingItemTypes".

asp 核心应用程序中似乎没有加载相关的架构文件.如何强制加载相关架构?

It seems that related schema files are not loaded in the asp core app. How can I force loading of related schemas ?

架构是:

base.xsd

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema
    targetNamespace="http://some.domain.org"
    xmlns="http://some.domain.org"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified">

    <xs:include id="enums" schemaLocation="enums.xsd"/>

    <xs:complexType name="tAccountingLines">
      <xs:sequence>
        <xs:element name="AccountingLine" type ="tAccountingLine"></xs:element>
      </xs:sequence>
    </xs:complexType>

    <xs:complexType name="tAccountingLine">
      <xs:sequence>
        <xs:element name="AccountingType" type="tAccountingItemTypes"></xs:element>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
</xs:schema>

枚举.xsd

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema
  targetNamespace="http://some.domain.org"
  xmlns="http://some.domain.org"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  elementFormDefault="qualified">

  <xs:simpleType name="tAccountingItemTypes">
    <xs:restriction base="xs:string">
      <xs:enumeration value="V1"/>
      <xs:enumeration value="V2"/>
      <xs:enumeration value="V3"/>
    </xs:restriction>
  </xs:simpleType>
</xs:schema>

推荐答案

我刚刚试过这个,它不加载包含的架构的原因是它用来加载它的解析器是 null.这应该可以解决它:

I've just tried this, and the reason it doesn't load the included schema is that the resolver it uses to load it is null. This should fix it:

schemas.XmlResolver = new XmlUrlResolver();

我进行了一些挖掘,发现这是 Deskop 和此处记录的核心:

I've done a bit of digging and found that this is a known behavioural change between Deskop & Core that is documented here:

如果添加的架构通过外部 URI 导入另一个架构,Core 不允许默认解析该 URI,而桌面允许.要允许在 Core 上解析,需要在添加架构之前调用以下内容:AppContext.SetSwitch("Switch.System.Xml.AllowDefaultResolver", true);

显然,除了开关之外,您还可以明确设置解析器,以便不使用默认值.

Obviously, in addition to the switch you can explicitly set a resolver so that you're not using the default.

这篇关于用于 xml 架构编译的 Full Framework 和 .NET Core 的不同行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-21 02:47