解决
<xs:import>
元素中指定。我以为它可能是一个访问问题,所以我将外部文档的一个副本移动到本地主机文件夹。我得到了同样的错误,所以现在我想知道是否使用
<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>
我试图与运行该代码是真正的简单
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())
{
}
一切顺利,直到(从http://dotnetslackers.com/Community/blogs/haissam/archive/2008/11/06/validate-xml-against-xsd-xml-schema-using-c.aspx得到它)该行在while循环之前实例化XMLReader对象。然后它失败,类型没有声明错误。它试图找到的类型KeyInfoType在import元素的其中一个文档中定义。我确定命名空间是一致的。我想知道命名空间定义中的#号是否引起问题,但删除它们没有效果,它只是改变了错误的样子(即“类型'http://www.w3.org/2000/09/xmldsig:KeyInfoType'未声明。”与“类型'http://www.w3.org/2000/09/xmldsig#:KeyInfoType'不是声明。“)
我怀疑是有一些关于<xs:import>
元素我缺少的处理。任何建议都非常受欢迎。谢谢!
我试过这一行,但没有任何效果。我看着ProcessSchemaLocation标志,发现的例子,我已经看到了使用复合运算符(新的我): settings.ValidationFlags | = System.Xml.Schema.XmlSchemaValidationFlags.ProcessSchemaLocation; 不幸的是,也没有工作。我想,我会继续阻止它。谢谢! – BobC 2009-05-01 21:27:41