Exemple #1
0
        public void Read(XmlReader reader)
        {
            //Add the attributes that should only be used during read phase
            //These attributes are kept for compatibility with previous versions
            //They should never been used during write process
            var attrs = new SpecificReadAttributes();

            attrs.Build();

            // Create an instance of the XmlSerializer specifying type and read-attributes.
            XmlSerializer serializer = new XmlSerializer(typeof(TestSuiteXml), attrs);

            using (reader)
            {
                // Use the Deserialize method to restore the object's state.
                TestSuite = (TestSuiteXml)serializer.Deserialize(reader);
            }
        }
Exemple #2
0
        protected virtual void Read(XmlReader reader)
        {
            //Add the attributes that should only be used during read phase
            //These attributes are kept for compatibility with previous versions
            //They should never been used during write process
            var attrs = new SpecificReadAttributes();

            attrs.Build();

            // Create an instance of the XmlSerializer specifying type and read-attributes.
            try
            {
                validationExceptions.Clear();
                XmlSerializer serializer = new XmlSerializer(typeof(TestSuiteXml), attrs);

                // Use the Deserialize method to restore the object's state.
                TestSuite = (TestSuiteXml)serializer.Deserialize(reader);
            }
            catch (InvalidOperationException ex)
            {
                if (ex.InnerException is XmlException)
                {
                    XmlSchemaException xmlSchemaException;
                    if (ex.InnerException.Message.Contains("For security reasons DTD is prohibited"))
                    {
                        xmlSchemaException = new XmlSchemaException("DTD is prohibited. To activate it, set the flag allow-dtd-processing to true in the config file associated to this test-suite");
                    }
                    else
                    {
                        var regex = new Regex(@"Line (\d+), position (\d+).$");
                        var match = regex.Match(ex.InnerException.Message);
                        if (match.Success)
                        {
                            int line = 0;
                            Int32.TryParse(match.Groups[1].Value, out line);
                            int position = 0;
                            Int32.TryParse(match.Groups[2].Value, out position);
                            xmlSchemaException = new XmlSchemaException(ex.InnerException.Message, ex, line, position);
                        }
                        else
                        {
                            xmlSchemaException = new XmlSchemaException(ex.InnerException.Message);
                        }
                    }
                    Console.WriteLine(xmlSchemaException.Message);
                    validationExceptions.Add(xmlSchemaException);
                }
                else
                {
                    ParseCascadingInvalidOperationException(ex.InnerException as InvalidOperationException);
                }
            }

            if (validationExceptions.Count > 0)
            {
                var message = "The test suite is not valid. Check with the XSD.";
                message += string.Format(" {0} error{1} {2} been found during the validation of the test-suite:\r\n"
                                         , validationExceptions.Count
                                         , validationExceptions.Count > 1 ? "s" : string.Empty
                                         , validationExceptions.Count > 1 ? "have" : "has");

                foreach (var error in validationExceptions)
                {
                    message += string.Format("\tAt line {0}: {1}\r\n", error.LineNumber, error.Message);
                }

                throw new ArgumentException(message);
            }
        }