edit我做了一个简化的回购协议https://github.com/GilShalit/XMLValidation https://github.com/GilShalit/XMLValidation
我正在 Blazor WebAssembly (TargetFramework=net5.0) 中构建 XML 编辑器。部分功能涉及验证 XML 的完整性并根据具有三个包含的复杂 xsd 架构进行验证。
这些是我遵循的步骤:
- 构建一个 XmlSchemaSet 并通过为每个 xsd 调用以下方法向其中添加 4 个架构:
private async Task loadSchema(string path, string nameSpace)
{
byte[] byteArrayS = await _client.GetByteArrayAsync(path);
Console.WriteLine($"{path}: {byteArrayS.Length}");
MemoryStream streamS = new MemoryStream(byteArrayS);
XmlReader xmlSchemaReader = XmlReader.Create(streamS);
schemaSet.Add(nameSpace, xmlSchemaReader);
}
- 使用以下命令初始化事件处理程序:
ValidationEventHandler eventHandler = new ValidationEventHandler(ValidationEventHandler);
- 将 XML 加载到 XmlDocument 中:
byte[] byteArrayX = Encoding.ASCII.GetBytes(await _editorTarget.GetValue());
MemoryStream streamX = new MemoryStream(byteArrayX);
XmlReader reader = XmlReader.Create(streamX);
XmlDocument document = new XmlDocument();
document.Load(reader);
- 根据 schemaSet 进行验证:
document.Schemas = schemaSet;
document.Validate(eventHandler);
步骤 3 和 4 在 Try...Catch 块内运行,并在 XML 格式不正确(例如缺少结束标记)时在本地运行,document.Load(reader);
该行会产生一条错误消息,如下所示:
The 'publicationStmt1' start tag on line 9 position 11 does not match the end tag of 'publicationStmt'. Line 11, position 12.
这太棒了。但在部署到 Azure 的应用程序中验证类似情况会产生以下错误消息:Xml_MessageWithErrorPosition, Xml_TagMismatchEx, 11, 12
.
当该行时,事件处理程序中会捕获架构验证错误document.Validate(eventHandler);
运行后,典型的消息是:
The element 'fileDesc' in namespace 'http://www.tei-c.org/ns/1.0' has invalid child element 'publicationStmt1' in namespace 'http://www.tei-c.org/ns/1.0'. List of possible elements expected: 'editionStmt, extent, publicationStmt' in namespace 'http://www.tei-c.org/ns/1.0'.
但是当在 Azure 上运行时,消息是Sch_InvalidElementContentExpecting
.
本地运行和 Azure 中运行的验证结果存在差异的原因是什么?
我尝试通过添加以下内容来禁用链接:
<ItemGroup>
<BlazorLinkerDescriptor Include="LinkerConfig.xml" />
</ItemGroup>
但这对已部署的应用程序没有任何影响,并且使用 Release 而不是 Debug 在本地运行也没有任何改变。
我还确保从 Azure 运行时实际加载了 4 个 xsd 文件。