vojta
vojta

Reputation: 5661

"Type 'http://www.w3.org/2000/09/xmldsig#:SignatureType' is not declared" in XmlDocument.Validate(...)

I have this very simple XSD schema

<?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>

stored as an embedded resource of my Visual Studio 2010 C# project as well as xmldsig-core-schema.xsd, which I downloaded from www.w3.org/TR/xmldsig-core/xmldsig-core-schema.xsd.

I would like to validate my document against this XSD schema. My document:

<?xml version="1.0" encoding="UTF-8"?>
<message xmlns="http://my.domain/xmlschemas/message">
</message>

I use XmlDocument.Validate(...) method for validation this way:

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;
});

This code throws an exception in doc.Validate(...) with message: Type 'http://www.w3.org/2000/09/xmldsig#:SignatureType' is not declared. However, there is no warning or error in Visual Studio XML Editor and I can see SignatureType in Visual Studio XML Schema Explorer. Why is this exception thrown? What should I do?

Upvotes: 2

Views: 3423

Answers (1)

vojta
vojta

Reputation: 5661

I solved the problem by myself. This line of my XSD did not work well:

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

I thought doc.Validate(...) would download or find all referenced external schemas automatically. (xmldsig-core-schema.xsd in my case). Well... it would not.

I had to add the referenced schema manually to doc.Schemas and since then it has been OK.

Resulting code:

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;
});

Upvotes: 1

Related Questions