Validate SOAP against XSD Schema?
I have a SOAP web service that was based on an XSD Schema (the schema generated the classes that was used as the input parameter for the web service method), as such:
public class CMService : WebService
{
[WebMethod(Description = "Submit trades")]
public bool SubmitTrades(List<TradesTrade> trades)
{
// Validation, if true开发者_如何学C, return true, else, return false;
return true;
}
}
How can I validate was passed in against the schema (In this case, the schema class is TradesTrades)?
Thanks.
It's not easy to do this, and may not be worthwhile.
Consider that if the XML being sent to your service doesn't match the schema, then it will not deserialize properly. If it is bad enough, your service operation won't even be called.
That said, if you really need to do this, then you should look at the example of a SoapExtension class. I recommend that you first get the example working exactly as-is. Then, I recommend you create a new version of the example, and make it do what you want.
What you want is to modify the WriteInput and/or WriteOutput methods to validate your XML using one of the available methods, perhaps by configuring an XmlReader to do the validation and to read from the input stream; and configuring an XmlWrite to write to the output stream; and then running a loop to read from the input and write to the output.
I've manually validate against the fields instead :)
I have used XML beans (xml binding framework) in my previous project. We created the xml schema and then generated the xml beans object from the schema's. These XML beans object have a lot of convenient methods to check the validity of the xml and the values passed in as a part of the XML.
Please let me know, If you have any particular question on XML beans.
I been having the same problem myself, the answer is that its possible to do this without needing to manually validate all the fields (which is error prone, plus since you have the schema already you may as well make use of it).
See this a article on the topic.
Basically, the process to follow is to first read the original Request.InputStream into an XmlDocument and then apply your schema and validation to the SOAP body inside it.
[WebMethod(Description = "Echo Soap Request")]
public XmlDocument EchoSoapRequest(int input)
{
// Initialize soap request XML
XmlDocument xmlSoapRequest = new XmlDocument();
XmlDocument xmlSoapRequestBody = new XmlDocument();
// Get raw request body
HttpContext httpContext = HttpContext.Current;
Stream receiveStream = httpContext.Request.InputStream
// Move to begining of input stream and read
receiveStream.Position = 0;
using (StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8))
{
// Load into XML document
xmlSoapRequest.Load(readStream);
}
// Now we have the original request, strip out the request body
foreach (XmlNode node in xmlSoapRequest.DocumentElement.ChildNodes)
{
if (node.NodeType == XmlNodeType.Element && node.LocalName == "Body" && node.FirstChild != null)
{
xmlSoapRequestBody.LoadXml(node.FirstChild.InnerXml);
}
}
// Validate vs Schema
xmlSoapRequestBody.Schemas.Add("http://contoso.com", httpContext.Server.MapPath("MySchema.xsd"))
xmlSoapRequestBody.Validate(new ValidationHandler(MyValidationMethod));
}
精彩评论