简体   繁体   English

如何针对定义该服务应遵循的xsd模式文件验证从WCF服务返回的数据

[英]How can I validate data returned from a WCF service, against xsd schema files that define what the service should conform to

I have some XSD files and a WSDL provided by the customer. 我有一些XSD文件和客户提供的WSDL。 I have built a WCF service. 我已经建立了WCF服务。 I would like to show that the service is compliant with the provided schemas. 我想表明该服务符合所提供的架构。 How can I validate my WCF service against those schemas? 如何针对这些架构验证WCF服务? I have SoapUI and Altova XML Spy if that helps. 如果有帮助,我有SoapUI和Altova XML Spy。 What is the standard way to do this? 这样做的标准方法是什么? I did not build this with the schema first approach. 我没有使用架构优先方法构建它。

Use this to pass XML and XSD. 使用它来传递XML和XSD。 This applies to any XML and Sxhema and not specific to web services: 这适用于任何XML和Sxhema,并且不适用于Web服务:

    private static void ValidateSchema(string xmlName, string schemaName)
    {

        try
        {
            ValidationEventHandler validationHandler = new ValidationEventHandler(ValidationCallBack);
            XmlTextReader schemaReader = new XmlTextReader( schemaName);
            XmlSchema schema =XmlSchema.Read(schemaReader, validationHandler);

            XmlTextReader docReader = new XmlTextReader (xmlName);
            XmlValidatingReader vr = new XmlValidatingReader( docReader); 
            vr.Schemas.Add (schema);  
            vr.ValidationType = ValidationType.Schema;
            vr.ValidationEventHandler += new ValidationEventHandler( ValidationCallBack);

            try 
            {
                while (vr.Read())
                {
                ;   //Console.Write(" {0} = \"{1}\";", vr.Name, vr.Value);
                }
            }

            catch 
            {
                //Console.WriteLine("Validation error!");

            }
        }


        catch(Exception ex)
        {
            Console.WriteLine(ex); 
        }

    }

    private static void ValidationCallBack(object sender, ValidationEventArgs e) 
    {
        Console.WriteLine("Validation Error: {0}", e.Message);
        Console.WriteLine("-------------------------------------------");
    }       

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM