针对在C#中包含并导入的XSD验证xml [英] Validating xml against an xsd that has include and import in c#

查看:105
本文介绍了针对在C#中包含并导入的XSD验证xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想缓存xsd,然后对其进行验证,而不是每次都为任何xml加载xsd以提高性能。但是,我无法做到这一点。我的猜测是,编译未添加xsd文件的include和import元素,这就是为什么我会在下面看到错误。

I would like to cache the xsd then perform validation against it rather than loading xsd every time for any xml in order to increase performance. However, I could not manage to do it. My guess is that the compilation does not add the include and import elements of the xsd files that is why I get the error below.

以下是步骤:

首先,我将 xsd 文件添加到 XmlSchemaSet

First I added the xsd file to XmlSchemaSet

public XmlSchemaSet SchemaSet;
public XmlValidator()
{
    this.SchemaSet = new XmlSchemaSet();
    using (XmlReader xsd = XmlReader.Create(xsdPath))
    {
        this.SchemaSet.Add(null, xsd);
    }
    this.SchemaSet.Compile();
}

然后我使用了 XmlSchemaSet 如下所示验证xml:

Then I used this XmlSchemaSet to validate the xml as follows:

public void ValidateSchema(byte[] xml)
{
    XmlReaderSettings settings = new XmlReaderSettings();
    settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation;
    settings.ValidationType = ValidationType.Schema;
    settings.DtdProcessing = DtdProcessing.Parse;

    // need to add dtd first. it should not be added to the schema set above
    using (XmlReader dtd = XmlReader.Create(dtdPath, settings))
    {
        settings.Schemas.Add(null, dtd);
    }

    settings.DtdProcessing = DtdProcessing.Prohibit;
    settings.Schemas.Add(this.SchemaSet); // add the schema set

    using (MemoryStream memoryStream = new MemoryStream(xml, false))
    {
        using (XmlReader validator = XmlReader.Create(memoryStream, settings))
        {
            while (validator.Read());
        }
    }
}

注意: dtdPath xsdPath 有效,输入xml有效,xsd文件有效

Notes: dtdPath, xsdPath are valid, input xml is valid, xsd files are valid

错误是:
未声明'http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader:StandardBusinessDocument'元素。

推荐答案

我用以下选项创建了 XmlReaderSettings

XmlReaderSettings settings = new XmlReaderSettings();
settings.XmlResolver = new XmlXsdResolver();     // Need this for resolving include and import
settings.ValidationType = ValidationType.Schema; // This might not be needed, I am using same settings to validate the input xml
settings.DtdProcessing = DtdProcessing.Parse;    // I have an include that is dtd. maybe I should prohibit dtd after I compile the xsd files.

然后我将它与 XmlReader 一起使用读取xsd。重要的部分是我必须放置 basePath 以便 XmlXsdResolve 可以找到其他xsd文件。

Then I used it with an XmlReader to read the xsd. The important part is that I had to put a basePath so that the XmlXsdResolve can find other xsd files.

using (XmlReader xsd = XmlReader.Create(new FileStream(xsdPath, FileMode.Open, FileAccess.Read), settings, basePath))
{
    settings.Schemas.Add(null, xsd);
}

settings.Schemas.Compile();

这是 XmlXsdResolver 来查找包含和导入的xsd文件:

This is the XmlXsdResolver to find included and imported xsd files:

protected class XmlXsdResolver : XmlUrlResolver
{
    public override object GetEntity(Uri absoluteUri, string role, Type ofObjectToReturn)
    {
        return base.GetEntity(absoluteUri, role, ofObjectToReturn);
    }
}

这篇关于针对在C#中包含并导入的XSD验证xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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