This project is read-only.

Validation against WSDL/XSDs in the service host

Feb 19, 2013 at 5:47 PM
Hi all,

Apologies for the very "newb" question but I think I'm probably missing something fairly fundamental.

I've designed my XSDs and have a simple starter project up and running but there doesn't appear to be any validation against schema as data is passed into my service host. For example I have a number of minoccurs=1 and nillable=false xs:string fields but if I fail to supply the data in my test client there is nothing in the server to notify me that there is a problem with the data.

I have also used some maxlength restrictions in my XSDs so know I cannot rely on proxy code alone for validation.

So how do I validate the incoming XML within my service host to ensure it does indeed conform to the WSDL/XSDs?

Many thanks in advance.

Feb 21, 2013 at 9:29 AM
Edited Feb 21, 2013 at 9:31 AM
We do the following:

Embed The XSD files in a resx file (We have a pre-build exe that scans the "Schemas" folder and spits out the resx file)
Add an extensible "Validate()" method that handles base validation against the schema:
            ValidationEventHandler handler = (o, e) => yourEventHandlingHere();

            Func<byte[], XmlSchema> loadSchema = s => XmlSchema.Read(new XmlTextReader(new MemoryStream(s)) { XmlResolver = new YourXmlResolverHere() }, handler);

            XmlSchemaSet schemas = new XmlSchemaSet();

            XmlSchema schema = loadSchema((byte[])Schemas.ResourceManager.GetObject(schemaName));
            XmlSchema types = loadSchema(Schemas.types);
            XmlSchema xmldsig = loadSchema(Schemas.xmldsig_core_schema);
            types.Includes.Add(new XmlSchemaImport() { Schema = xmldsig, Namespace = "" });
            schema.Includes.Add(new XmlSchemaInclude() { Schema = types});

            var schemaElement = schema.Elements[new XmlQualifiedName(this.Untyped.Name.LocalName, this.Untyped.Name.Namespace.ToString())];
            this.Untyped.Validate(schemaElement, schemas, handler, true);
Read this on how to resolve related XSD files that are embedded (xsd:import/include)
Write a class that handles your situation in place of "YourXmlResolverHere()"