cui chun
cui chun

Reputation: 107

Cannot validate against multiple xsd schemas in C#

I wanna verify a digitally signed xml against its schema definition while this schema actually contains this tag

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

Then I tried to load schemas:

XmlReaderSettings settings = new XmlReaderSettings();
settings.Schemas.Add(null, "a.xsd");
settings.Schemas.Compile();

I will get the following error The 'http://www.w3.org/2000/09/xmldsig#:Signature' element is not declared.

Upvotes: 2

Views: 6313

Answers (4)

Rodrigo Gomez I.
Rodrigo Gomez I.

Reputation: 21

The scheme xmldsig-core-schema.xsd does not charge for security reasons since it makes reference to a DTD to validate the upload directory and add it as another scheme.

<!DOCTYPE schema PUBLIC "-//W3C//DTD XMLSchema 200102//EN" "http://www.w3.org/2001/XMLSchema.dtd"

this works The solution is C#

XElement xsdMarkup = XElement.Load("C:\\Proyectos\\WindowService\\Sbif\\Schema\\Schema\\IndicadoresFinancieros-v1.0.xsd");

XElement xsdMarkup2 = XElement.Load("http://www.w3.org/TR/2002/REC-xmldsig-core-20020212/xmldsig-core-schema.xsd");

XmlSchemaSet schemas = new XmlSchemaSet();

schemas.Add(null, xsdMarkup.CreateReader());
schemas.Add(null, xsdMarkup2.CreateReader());

schemas.Compile();

Upvotes: 2

user11937
user11937

Reputation:

You need to also load in the imported schema with another

settings.Schemas.Add([importednamespace], [pathtoimportedXSD]);

Upvotes: 2

Richard
Richard

Reputation: 108995

From the error it would seem the XML Signature schema is not being loaded, despite the import.

Adding the XML Signature schema to the schema set explicitly should confirm that.

The most likely cause is the schema set's XmlReslver is not finding the file you specify, this could be a current folder/relative path issue.

Using Process Monitor to see where you could is trying to load the XSD file may also help.

Upvotes: 0

dusoft
dusoft

Reputation: 11479

are you sure hash is required at the end of?: http://www.w3.org/2000/09/xmldsig#

Upvotes: 0

Related Questions