Microsoft.Protocols.TestSuites.Common.ActiveSyncClient.ValidateResponseSchema C# (CSharp) Method

ValidateResponseSchema() public method

The schema validation for the XML string.
public ValidateResponseSchema ( string decodeXMLstring, ITestSite testSite ) : void
decodeXMLstring string The XML string which is decoded from WBXML format
testSite ITestSite An instance of interface ITestSite which provides logging, assertions, and adapters for test code onto its execution context.
return void
        public void ValidateResponseSchema(string decodeXMLstring, ITestSite testSite)
        {
            if (string.IsNullOrEmpty(decodeXMLstring))
            {
                this.validationResult = true;
                return;
            }

            this.lastRawString = decodeXMLstring;

            // Initialize Validation Result Recorder
            this.validationResult = false;
            this.xmlValidationWarnings = new Collection<ValidationEventArgs>();
            this.xmlValidationErrors = new Collection<ValidationEventArgs>();

            // Prepare settings of XML reader
            XmlReaderSettings settings = new XmlReaderSettings();
            foreach (string xmlSchema in this.XmlSchemaList)
            {
                using (StringReader stringReader = new StringReader(xmlSchema))
                {
                    settings.Schemas.Add(GetTargetNamespace(xmlSchema), XmlReader.Create(stringReader));
                }
            }

            settings.ValidationType = ValidationType.Schema;
            settings.ConformanceLevel = ConformanceLevel.Document;
            settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
            settings.ValidationEventHandler += new ValidationEventHandler(this.ValidationCallBack);

            // Load into the memory stream
            using (MemoryStream ms = new MemoryStream(System.Text.ASCIIEncoding.UTF8.GetBytes(this.lastRawString)))
            {
                // Create XML reader for SOAP client message
                XmlReader xmlReader = XmlReader.Create(ms, settings);

                while (xmlReader.Read())
                {
                }
            }

            this.validationResult = this.xmlValidationErrors.Count == 0 && this.xmlValidationWarnings.Count == 0;

            if (!this.validationResult)
            {
                string errorInformation = null;

                string warningInformation = null;

                if (this.xmlValidationErrors.Count > 0)
                {
                    if (this.xmlValidationErrors.Count == 1)
                    {
                        errorInformation = string.Format("There is a schema validation error:\r\n");
                    }
                    else
                    {
                        errorInformation = string.Format("There are {0} schema validation errors:\r\n", this.xmlValidationErrors.Count);
                    }

                    for (int i = 0; i < this.xmlValidationErrors.Count; i++)
                    {
                        errorInformation = errorInformation + string.Format("Error: {0}\r\n", this.xmlValidationErrors[i].Message);
                    }
                }

                if (this.xmlValidationWarnings.Count > 0)
                {
                    if (this.xmlValidationWarnings.Count == 1)
                    {
                        warningInformation = string.Format("There is a schema validation warning:\r\n");
                    }
                    else
                    {
                        warningInformation = string.Format("There are {0} schema validation warnings:\r\n", this.xmlValidationWarnings.Count);
                    }

                    for (int i = 0; i < this.xmlValidationWarnings.Count; i++)
                    {
                        warningInformation = warningInformation + string.Format("Warning: {0}\r\n", this.xmlValidationWarnings[i].Message);
                    }
                }

                testSite.Assert.Fail(errorInformation + warningInformation);
            }
        }