对XSD模式进行验证?
-
10-10-2019 - |
题
我有一个基于XSD架构的SOAP Web服务(该模式生成了用作Web服务方法的输入参数的类),因此:
public class CMService : WebService
{
[WebMethod(Description = "Submit trades")]
public bool SubmitTrades(List<TradesTrade> trades)
{
// Validation, if true, return true, else, return false;
return true;
}
}
我该如何验证通过模式传递(在这种情况下,架构类是 交易)?
谢谢。
解决方案 2
我已经手动验证了这些领域:)
其他提示
这样做并不容易,也可能不值得。
考虑一下,如果发送到您的服务的XML与模式不匹配,则它将无法正确启动。如果足够糟糕,您的服务操作甚至都不会被调用。
也就是说,如果您真的需要这样做,那么您应该看看 soapextension 班级。我建议您首先获得该示例正常工作。然后,我建议您创建该示例的新版本,并使其完成您想要的工作。
您想要的是修改写入和/或写入方法方法,以使用可用方法之一验证XML,也许是通过配置XMLReader来进行验证并从输入流读取;并配置XMLWRITE以写入输出流;然后运行一个循环以从输入中读取并写入输出。
我用过 XML豆 (XML绑定框架)在我以前的项目中。我们创建了XML架构,然后从模式中生成XML Beans对象。这些XML Beans对象具有许多方便的方法来检查XML的有效性和作为XML的一部分传递的值。
如果您在XML豆子上有任何特定问题,请告诉我。
我本人遇到了同样的问题,答案是可以做到这一点而无需手动验证所有字段(这是错误的错误,而且由于您已经有了模式,因此您也可以使用它)。
基本上,遵循的过程是首先阅读原始 request.inputstream 进入Xmldocument,然后将您的架构和验证应用于其中的肥皂体。
[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));
}
不隶属于 StackOverflow