根据架构验证 XML 文件引发异常 [英] Validating XML file against schema throws exception

查看:33
本文介绍了根据架构验证 XML 文件引发异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试根据 MVC/ASP.Net Core 中的架构验证 XML 文件时遇到异常错误.

I'm running into an Exception error when trying to validate an XML file against a schema in MVC/ASP.Net Core.

我得到的错误是这样的:

The error I'm getting is this:

System.Xml.Schema.XmlSchemaValidationException: 'Type 'http://ns.editeur.org/onix/3.0/reference:SourceTypeCode' 未声明,或者不是简单类型.'

System.Xml.Schema.XmlSchemaValidationException: 'Type 'http://ns.editeur.org/onix/3.0/reference:SourceTypeCode' is not declared, or is not a simple type.'

我的代码是这样的:

XmlSchemaSet onixschema = new XmlSchemaSet();

XDocument xmlDocument = XDocument.Load(@"path.to.xml.file");

onixschema.Add("http://ns.editeur.org/onix/3.0/reference", @"path.to.ONIX_BookProduct_3.0_reference.xsd");

xmlDocument.Validate(onixschema, (o, e) =>
        {
            validationResult.ErrorMessages.Add(e.Message);
        });

我真的不知道从哪里开始处理异常.该消息并没有太大的启发性!

I don't really know where to start with the exception. The message isn't wildly enlightening!

真正令人困惑的是,如果我在 WebForms/.Net(相同文件等)中运行这个确切的代码,它会正确验证.

And the really baffling thing is that if I run this exact code in WebForms/.Net (same files, etc), it validates correctly.

我没有收到错误信息.

如果有人能够对此有所了解,我将不胜感激.

If anybody was able to shed some light on this, I'd be very grateful.

//编辑

很确定它与 ASP.Net Core 有关系.如果我创建一个全新的 MVC/.Net Web 应用程序,并将代码粘贴到 Home Controller 中,它就会正常工作.没有验证异常.

Pretty sure it has something to do with ASP.Net Core. If I create a brand new MVC/.Net web app, and stick the code in the Home Controller, it works as it should. No validation exception.

如果我创建一个全新的 MVC/ASP.NET Core 应用程序,并将代码粘贴到主控制器中,它会失败,并出现验证异常.

If I create a brand new MVC/ASP.NET Core App, and stick the code in the Home Controller, it fails, with the validation exception.

是否与 ASP.NET Core 处理静态 XSD 文件的方式有关?

Could it be to do with the way ASP.NET Core handles the static XSD file?

推荐答案

好的,我已经回答了这个问题.

Okay, I've sort of answered the question.

问题是 XSD 文件导入了另外两个 XSD 文件.如果我手动导入这些创建一个更大的 XSD 文件,那么问题就会消失.

The problem was that the XSD file imports two further XSD files. If I import these manually creating a much larger XSD file, then the problem goes away.

所以这与 ASP.NET Core 处理相对路径的方式有关.它没有定位到要导入的两个 XSD 文件,或者找不到它们(而 Web Forms & MVC/.NET 能够这样做).我还不知道为什么!

So it's got something to do with the way ASP.NET Core handles relative paths. It's not locating recognizing that there are two XSD files to be imported, or it can't find them (whereas Web Forms & MVC/.NET is able to do so). I don't know why yet!

这篇关于根据架构验证 XML 文件引发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆