我自己也遇到过同样的问题,答案是它可以在不需要手动验证所有字段的情况下做到这一点(这很容易出错,而且既然你已经有了架构,你也可以使用它)。
请参阅有关该主题的文章。
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));
}