I have some WCF methods that are used to transmit information from a server application to a website frontend for use in binding. I’m sending the result as an XElement that is a root of an XML tree containing the data I want to bind against.
I’d like to create some tests that examine the data and ensure it comes across as expected.
My current thinking is this: Every method that returns an XElement tree has a corresponding schema (.XSD) file. This file is included within the assembly that contains my WCF classes as an embedded resource.
Tests call the method on these methods and compares the result against these embedded schemas.
Is this a good idea? If not, what other ways can I use to provide a ‘guarantee’ of what kind of XML a method will return?
If it is, how do you validate an XElement against a schema? And how can I get that schema from the assembly it’s embedded in?
Id say validating xml with a xsd schema is a good idea.
How to validate a XElement with the loaded schema: As you see in this example you need to validate the XDocument first to get populate the ‘post-schema-validation infoset’ (There might be a solution to do this without using the Validate method on the XDOcument but Im yet to find one):
How to read the embedded schema from an assembly and add it to the XmlSchemaSet: