【问题标题】:"Type 'http://www.w3.org/2000/09/xmldsig#:SignatureType' is not declared" in XmlDocument.Validate(...)XmlDocument.Validate(...) 中的“未声明类型'http://www.w3.org/2000/09/xmldsig#:SignatureType'”
【发布时间】: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


    【解决方案1】:

    我自己解决了这个问题。我的 XSD 的这行不太好用:

    <import namespace = "http://www.w3.org/2000/09/xmldsig#" schemaLocation = "xmldsig-core-schema.xsd"/>
    

    我认为doc.Validate(...) 会自动下载或查找所有引用的外部模式。 (在我的情况下是xmldsig-core-schema.xsd)。嗯……不会的。

    我不得不手动将引用的架构添加到doc.Schemas,从那以后就可以了。

    结果代码:

    XmlDocument doc = new XmlDocument();
    doc.PreserveWhitespace = true;
    doc.Load(inputStream);
    
    Assembly myAssembly = Assembly.GetExecutingAssembly();
    foreach (string resource in new string[] {"message.xsd", "xmldsig-core-schema.xsd"}) {
          using (Stream schemaStream = myAssembly.GetManifestResourceStream("XmlSigTest.Resources." + resource))
          {
                 XmlSchema schema = XmlSchema.Read(schemaStream, null);
                 doc.Schemas.Add(schema);
          }
    }
    
    bool ok = true;
    doc.Validate((s, e) =>
    {
         ok = false;
    });
    

    【讨论】:

      猜你喜欢
      • 2013-06-08
      • 1970-01-01
      • 1970-01-01
      • 2017-01-27
      • 2011-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多