While developing our web service client, I received a message from the server that did not deserialize properly. It was rather large, and just by looking at it, I could not figure out what was wrong. It was obvious it is not conforming to the schema the client expects, but where?
I Googled it, and found that many people ask that same question: how do I check a SOAP message against WSDL? Surprisingly, there was no simple answer. I ended up checking the message in Eclipse IDE (we do some Java development too, so I had one installed). However, it required manually altering the message to add appropriate schema locations.
At this point, I decided to write a simple validator that would do this job automatically. Thanks to .NET built-in XML validation functionality, it is not very hard to do: all you need is to parse arguments and supply proper input to a validating XML reader.
Using the Code
Since downloading executables is forbidden in many companies, and compiling an executable is a hassle, I created the validator as a self-compiling .BAT file. Beginning of the file has some command line instructions for compilation, the rest is C# 2.0 source code. All you need is the .BAT file and .NET framework 2.0 or higher.
Since CodeProject does not allow uploading .BAT files, I renamed it to .TXT. Rename it back to .BAT after download. A precompiled executable is also available.
Using the code is straightforward:
SoapValidator message.xml contract.wsdl schema1.xsd schema2.xsd...
All arguments except for the message file name are optional. If you supply "-" for file name, this means "read standard input".
The validator can check any XML document, not just SOAP messages. To make SOAP validation easier, the schema for the SOAP format itself (envelope, body, header) is built into the validator.
In additional to XSD schema files, the validator recognizes WSDL files with
<schema> tags. If any schemas are imported, they must have valid
schemalocation accessible to the validator.
Points of Interest
Self-compiling batch files or scripts are not a regular thing in .NET, while they are very common in UNIX. I find them useful, because they are not executable code per se (easier to get through firewalls and mail filters), and you don't need an IDE to compile them. I really wish Windows had the #! notation from UNIX, but alas.
- 16th April, 2011: Initial post
- 7th June, 2011: Replaced "
exit" with "
exit /B 0" on line 10 of SoapValidator.bat. This prevents it from closing the command prompt window.