c# - 在 C# 中编译包含重新定义的架构

标签 c# xml include schema redefinition

读取包含 <xs:redefine> 的架构时标签并尝试使用模式集编译它,我得到这个异常:

'SchemaLocation' must successfully resolve if <redefine>
contains any child other than <annotation>

我尝试了许多未成功的解决方法,例如递归解析模式并在编译之前将它们添加到模式集中,甚至将它们添加为引用。架构仍然无法编译。尝试过的示例(解析主 xsd,然后在调用此递归函数后尝试编译生成的“XmlSchemas”):

private void AddRecursive (XmlSchemas xsds, XmlSchema schema)
{
    foreach (XmlSchemaExternal inc in schema.Includes) {
        String schemaLocation = null;
        schemaLocation = inc.SchemaLocation;
        XmlSchema xsd;
        using (FileStream stream = new FileStream (schemaLocation, FileMode.Open, FileAccess.Read)) {
            xsd = XmlSchema.Read (stream, null);
            xsds.Add (xsd);
            xsds.AddReference (xsd);
        }
        AddRecursive (xsds, xsd);
    }
}

处理此类模式的正确方法是什么?为什么模式编译器不能自己解析添加的模式?

最佳答案

使用基于流的重载读取 XML 模式的问题是没有可供 XML 模式读取器使用的基本 uri。如果您的 xsd:redefine 在 schemaLocation 属性中使用相对 URI,则默认解析器将无法找到正在重新定义的模式 - 因此您会收到错误消息。

我正在为您提供以下配置,让您至少了解这是如何工作的。将两个架构和测试脚本保存在同一文件夹中,更新脚本中的路径并运行 C# 脚本。它会给你这个输出:

QN: http://tempuri.org/XMLSchema.xsd:TRedefine, SourceUri: file:///D:/.../.../Redefine.xsd
QN: http://www.w3.org/2001/XMLSchema:anyType, SourceUri: 

如果您更新脚本以使用基于流的重载,您将从您的帖子中收到错误消息。

Error line 20:      xset.Add(XmlSchema.Read(File.Open    (@"D:\...\...\Redefine.xsd", FileMode.Open), null));
    'SchemaLocation' must successfully resolve if <redefine> contains any child other than     <annotation>.
'SchemaLocation' must successfully resolve if <redefine> contains any child other than     <annotation>.
Error Line 21: xset.Add(XmlSchema.Read(File.Open    (@"D:\...\...\Redefine.xsd", FileMode.Open), null));

基本模式:

<?xml version="1.0" encoding="utf-8" ?>
<xsd:schema targetNamespace="http://tempuri.org/XMLSchema.xsd"
    elementFormDefault="qualified"
    xmlns="http://tempuri.org/XMLSchema.xsd"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:complexType name="TRedefine">
        <xsd:sequence>
            <xsd:element name="base" type="xsd:string"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:schema>

重新定义的架构:

<?xml version="1.0" encoding="utf-8"?>
<!--XML Schema generated by QTAssistant/XSR Module (http://www.paschidev.com)-->
<xsd:schema xmlns="http://tempuri.org/XMLSchema.xsd" attributeFormDefault="unqualified"     elementFormDefault="qualified" targetNamespace="http://tempuri.org/XMLSchema.xsd"     xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <!-- Put a full path here.
  <xsd:redefine schemaLocation="D:\...\...\Base.xsd">
    -->
    <xsd:redefine schemaLocation="Base.xsd">
        <xsd:complexType name="TRedefine">
            <xsd:complexContent>
                <xsd:extension base="TRedefine">
                    <xsd:sequence/>
                </xsd:extension>
            </xsd:complexContent>
        </xsd:complexType>
    </xsd:redefine>
</xsd:schema>

测试脚本:

using System;
using System.IO;
using System.Xml;
using System.Xml.Schema;
class Script
{
    public static void Main()
    {
        // Enter your code below
        // Generated by QTAssistant (http://www.paschidev.com)
        XmlSchemaSet xset = new XmlSchemaSet();

        // One way of doing using an XmlReader - it'll work with relative URIs.
        using(XmlReader reader = XmlReader.Create(@"D:\...\...\Redefine.xsd"))
        {
            xset.Add(XmlSchema.Read(reader, null));
        }

        // The other way, using stream, requires all external URIs - xsd:include,     xsd:import and xsd:redefine 
        // to be absolute
        //xset.Add(XmlSchema.Read(File.Open(@"D:\...\...\Redefine.xsd", FileMode.Open), null));

        xset.Compile();
        Console.WriteLine(xset.IsCompiled);
        foreach(XmlSchemaType type in xset.GlobalTypes.Values) 
        {
            Console.WriteLine("QN: {0}, SourceUri: {1}", type.QualifiedName,     type.SourceUri);
        }
    }
}

关于c# - 在 C# 中编译包含重新定义的架构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7877505/

相关文章:

c++ - "double link"头文件是否有任何有用或必要的情况? (C++)

c# - 有没有办法告诉编译器将始终执行特定代码块?

c# - 使用 NHibernate 重用线程的 session

mysql - 如何从脚本将xml插入mysql?

android - 如何显示水平和垂直渐变分隔线?

php - 包含 localhost 的绝对路径

c# - 转换varchar值时转换失败

c# - 如果我不知道 C# 中的结构,如何访问对象的内容?

python - 使用Python从xml中的一个节点遍历到另一个节点

PHP is_readable 问题与 open_basedir