我正在使用 XML 模式文档来验证传入的数据文档,但是该模式在运行时编译期间似乎失败了,因为它引用了外部模式的一部分的复杂类型。外部模式<xs:import>
在文档顶部的元素中指定。我曾认为这可能是访问问题,所以我将外部文档的副本移动到 localhost 文件夹。<xs:import>
我得到了同样的错误,所以现在我想知道使用该元素是否可能存在某种问题。
架构文档片段如下所示:
<xs:schema targetNamespace="http://www.smpte-ra.org/schemas/429-7/2006/CPL" xmlns:cpl="http://www.smpte-ra.org/schemas/429-7/2006/CPL" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
...
<xs:import namespace="http://www.w3.org/2000/09/xmldsig#" schemaLocation="http://localhost/TMSWebServices/XMLSchema/xmldsig-core-schema.xsd"/>
...
<xs:element name="Signer" type="ds:KeyInfoType" minOccurs="0"/>
...
</xs:schema>
我试图运行它的代码非常简单(从http://dotnetslackers.com/Community/blogs/haissam/archive/2008/11/06/validate-xml-against-xsd-xml-schema -使用-c.aspx)
string XSDFILEPATH = @"http://localhost/TMSWebServices/XMLSchema/CPL.xsd";
string XMLFILEPATH = @"C:\foo\bar\files\TestCPLs\CPL_930f5e92-be03-440c-a2ff-a13f3f16e1d6.xml";
System.Xml.XmlReaderSettings settings = new System.Xml.XmlReaderSettings();
settings.Schemas.Add(null, XSDFILEPATH);
settings.ValidationType = System.Xml.ValidationType.Schema;
System.Xml.XmlDocument document = new System.Xml.XmlDocument();
document.Load(XMLFILEPATH);
System.Xml.XmlReader rdr = System.Xml.XmlReader.Create(new StringReader(document.InnerXml), settings);
while (rdr.Read())
{
}
一切顺利,直到在 while 循环之前实例化 XMLReader 对象的行。然后它失败并出现类型未声明的错误。它试图查找的类型 KeyInfoType 是在 import 元素的文档之一中定义的。我已确保命名空间对齐。我想知道命名空间定义中的 # 符号是否引起了问题,但删除它们没有任何效果,它只是改变了错误的样子(即“Type ' http://www.w3.org/2000/09/xmldsig :KeyInfoType ' 未声明。”与“Type ' http://www.w3.org/2000/09/xmldsig#:KeyInfoType ' 未声明。”)
我怀疑<xs:import>
我缺少的元素的处理有些问题。任何建议都非常受欢迎。谢谢!