本文介绍了将 XSD 与包含一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个 XSD:

<?xml version="1.0"?>
<xsd:schema
elementFormDefault='unqualified'
attributeFormDefault='unqualified'
xmlns:xsd='http://www.w3.org/2001/XMLSchema'
>

  <xsd:simpleType name='TheSimpleType'>
    <xsd:restriction base='xsd:string' />
  </xsd:simpleType>
</xsd:schema>

这是第二个 XSD,包括上面的一个:

Here is a second XSD that includes the one above:

<?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema
elementFormDefault='unqualified'
attributeFormDefault='unqualified'
xmlns:xsd='http://www.w3.org/2001/XMLSchema'
targetNamespace='a'
xmlns='a'
>

  <xsd:include schemaLocation='Include.xsd' />

  <xsd:element name = "TheElement" >
  <xsd:complexType>
  <xsd:attribute name="Code" type="TheSimpleType" use="required"/>
  </xsd:complexType>
  </xsd:element>
</xsd:schema>

我需要将(第二个)XSD 读入 C# 并且:

I need to read the (second) XSD into C# and:

  1. 检查它是否是有效的 XSD,并且
  2. 根据它验证文档.

这里有一些 C# 可以在架构中阅读:

Here is some C# to read in the schemata:

    XmlSchemaSet schemaSet = new XmlSchemaSet();
    foreach (string sd in Schemas)
    {
        using (XmlReader r = XmlReader.Create(new FileStream(sd, FileMode.Open)))
        {
            schemaSet.Add(XmlSchema.Read(r, null));
        }
    }
    schemaSet.CompilationSettings = new XmlSchemaCompilationSettings();
    schemaSet.Compile();

.Compile() 失败,因为类型 'a:TheSimpleType' 未声明,或者不是简单类型."

The .Compile() fails because "Type 'a:TheSimpleType' is not declared, or is not a simple type."

但是,如果满足以下任一条件,它就会起作用:

However, it works if either:

  • 命名空间已从架构中移除,或者
  • 命名空间被添加到包含中.

问题是:如何在不编辑模式的情况下让 C# 接受它?

The question is: how do I get C# to accept it without editing the schemata?

我怀疑问题在于,虽然我已将两个模式都放入 XmlSchemaSet,但我仍然需要告诉 C# 一个包含在另一个中,即它自己并没有解决.事实上,如果我只告诉 XmlSchemaSet 主 XSD(而不是包含)(都没有(或有)命名空间),那么类型 'TheSimpleType' 没有声明,或者不是简单类型."

I suspect the problem is that although I have put both schemata into the XmlSchemaSet, I still need to tell C# that one is included into the other, i.e., it hasn't worked it out for itself. Indeed, if I only tell the XmlSchemaSet about the main XSD (and not the include) (both without (or with) namespaces) then "Type 'TheSimpleType' is not declared, or is not a simple type."

因此这似乎是一个关于解决的问题包括:如何?!

Thus this seems to be a question about resolving includes: how?!

推荐答案

问题在于在线打开模式读取的方式:

The problem is with the way the schema is opened for reading on the line:

XmlReader.Create(new FileStream(sd, FileMode.Open)

我必须编写自己的 XmlResolver 才能看到如何解析包含文件的路径:它来自可执行文件的目录,而不是来自父架构的目录.问题是父模式没有得到它的 BaseURI 集.以下是必须打开模式的方式:

I had to write my own XmlResolver before I could see how the paths to the include files were being resolved: it was from the directory of the executable and not from the directory of the parent schema. The problem is that the parent schema was not getting its BaseURI set. Here's how the schema must be opened:

XmlReader.Create(new FileStream(pathname, FileMode.Open, FileAccess.Read),null, pathname)

这篇关于将 XSD 与包含一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 20:45