开发者

Validate (and Query) XmlElement Content without OuterXML?

开发者 https://www.devze.com 2023-02-08 12:52 出处:网络
I have a Web Method (within a SOAP Web Service) with a signature of: public msgResponse myWebMethod([XmlAnyElement] XmlElement msgRequest)

I have a Web Method (within a SOAP Web Service) with a signature of:

public msgResponse myWebMethod([XmlAnyElement] XmlElement msgRequest)

I chose to use the XmlElement parameter after reading that it would allow me to perform my own XSD validation on the parameter. The problem is that the parameter can be quite large (up to 80Mb of XML) so calling XmlElement.OuterXML() as suggested in the link isn't a very practical method.

Is there another way to validate the XmlElement object against an XSD?

More generally, is this an inappropriate approach for implementing a web service expecting large amounts of XML? I've come across some hints at using SoapExtensions for gaining access to the input stream di开发者_运维知识库rectly but am not sure this is the correct approach for my situation.

Note: Unfortunately, I'm chained to an existing WSDL and XSD that I have no power to alter which is why I went with a non-WCF implementation in the first place.


Here's a quick example. Just pass your XmlElement to this method:

private static void TheAnswer(IXPathNavigable inputElement)
{
    var schemas = new XmlSchemaSet();
    schemas.Add("http://foo.org/importvalidator.xsd",
                @"..\..\validator.xsd");
    var settings = new XmlReaderSettings
                       {
                           Schemas = schemas,
                           ValidationFlags =
                               XmlSchemaValidationFlags.
                                   ProcessIdentityConstraints |
                               XmlSchemaValidationFlags.
                                   ReportValidationWarnings,
                           ValidationType = ValidationType.Schema
                       };
    settings.ValidationEventHandler +=
        (sender, e) =>
        Console.WriteLine("{0}: {1}", e.Severity, e.Message);
    using (
        XmlReader documentReader =
            inputElement.CreateNavigator().ReadSubtree())
    {
        using (
            XmlReader validatingReader = XmlReader.Create(
                documentReader, settings))
        {
            while (validatingReader.Read())
            {
            }
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消