下面是一个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包含上面的一个:
<?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#和:
- 检查,这是一个有效的XSD和
- 反对验证文档。
下面是一些C#中的图式阅读:
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()失败,因为 “类型。 '一:TheSimpleType' 未声明,或者不是一个简单的类型”
但是,它的工作原理如果任:
- 命名空间从架构移除或
- 命名空间被添加到包括。
问题是:如何让C#在不编辑模式的情况下接受它?
我怀疑问题是,虽然我已经把这两个模式放到XmlSchemaSet中,但我仍然需要告诉C#一个包含在另一个中,即它没有为它自己完成。事实上,如果我只告诉XmlSchemaSet有关主XSD(而不是包含)(都没有(或带有)命名空间),那么“Type'TheSimpleType'没有声明,或者不是简单类型。
因此,这似乎是一个关于解决包括的问题:怎么样?!
+1从来不知道“XmlSchemaInclude”类。很好的答案。 – psubsee2003 2012-04-12 17:53:17
好,好。但是现在假设我必须在运行时确定所有的包含,也就是说,我给你一个任意的包含XSD的XSD,你必须去获取它们。 – 2012-04-12 17:59:11
's = XmlSchema.Read(r,null);' 现在我看到我们有'.Includes',它们是'XmlSchemaInclude'对象,并且它的正确填充(包含1)。 – 2012-04-12 18:02:35