【发布时间】:2016-01-08 14:37:34
【问题描述】:
我有这个非常简单的 XSD 架构
<?xml version = "1.0" encoding = "UTF-8"?>
<schema xmlns = "http://www.w3.org/2001/XMLSchema"
targetNamespace = "http://my.domain/xmlschemas/message"
xmlns:mmm = "http://my.domain/xmlschemas/message"
xmlns:ds = "http://www.w3.org/2000/09/xmldsig#"
xmlns:xsd = "http://www.w3.org/2001/XMLSchema"
elementFormDefault = "qualified">
<import namespace = "http://www.w3.org/2000/09/xmldsig#" schemaLocation = "xmldsig-core-schema.xsd"/>
<element name = "message">
<complexType>
<sequence>
<element name = "Signature" type = "ds:SignatureType" minOccurs = "0" maxOccurs = "unbounded"/>
</sequence>
</complexType>
</element>
</schema>
存储为我的 Visual Studio 2010 C# 项目以及我从 www.w3.org/TR/xmldsig-core/xmldsig-core-schema.xsd 下载的 xmldsig-core-schema.xsd 的嵌入式资源。
我想根据这个 XSD 架构验证我的文档。我的文件:
<?xml version="1.0" encoding="UTF-8"?>
<message xmlns="http://my.domain/xmlschemas/message">
</message>
我使用XmlDocument.Validate(...)这种方式进行验证:
XmlDocument doc = new XmlDocument();
doc.PreserveWhitespace = true;
doc.Load(inputStream); //XML document loads correctly...
Assembly myAssembly = Assembly.GetExecutingAssembly();
using (Stream schemaStream = myAssembly.GetManifestResourceStream("XmlSigTest.Resources.message.xsd"))
{
XmlSchema schema = XmlSchema.Read(schemaStream, null);
doc.Schemas.Add(schema); //XSD schema loads correctly
}
bool ok = true;
doc.Validate((s, e) => //throws Exception!!!
{
ok = false;
});
此代码在doc.Validate(...) 中引发异常,消息为:Type 'http://www.w3.org/2000/09/xmldsig#:SignatureType' is not declared。但是,Visual Studio XML 编辑器中没有警告或错误,我可以在 Visual Studio XML Schema Explorer 中看到SignatureType。为什么会抛出这个异常?我该怎么办?
【问题讨论】:
标签: c# xml validation xsd xsd-validation