简体   繁体   中英

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. I have built a WCF service. I would like to show that the service is compliant with the provided schemas. How can I validate my WCF service against those schemas? I have SoapUI and Altova XML Spy if that helps. 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. This applies to any XML and Sxhema and not specific to web services:

    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("-------------------------------------------");
    }       

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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