public void TestXDocumentWithNoDataElementIsReportedAsAfailure()
        {
            var validator = new XmlValidator();
            var document  = LoadXmlDocument(@"../../Validator/XmlTestData/NoData.xml");
            var result    = validator.ValidateXml(document);

            Assert.IsFalse(result.Item1);
            Assert.AreEqual(Resources.NoDataElementsInDocument, result.Item2.First());
        }
        public void TestValidXmlPassesValidation()
        {
            var validator = new XmlValidator();
            var document  = LoadXmlDocument(@"../../Validator/XmlTestData/ValidXmlFile.xml");
            var result    = validator.ValidateXml(document);

            Assert.IsTrue(result.Item1);
            Assert.AreEqual(0, result.Item2.Count());
        }
예제 #3
0
        public EckValidationResult Validate(ValidationRequest request)
        {
            try
            {
                try
                {
                    request.XmlDocument = UTF8Encoding.UTF8.GetString(Convert.FromBase64String(request.XmlDocument));
                }
                catch
                {
                }



                Trace.WriteLine(DateTime.Now + "-> START Generate request = " + request.Name);



                var validationResult = new EckValidationResult();
                var validator        = new XmlValidator();
                validationResult.BrokenRules = validator.ValidateXml(request.XmlDocument,
                                                                     (ValidationProfile)
                                                                     Enum.Parse(typeof(ValidationProfile), request.Name,
                                                                                true));

                if (validationResult.BrokenRules.Count > 0)
                {
                    validationResult.IsValid = false;
                    validationResult.Message = "Correct errors and then try again.";
                }
                else
                {
                    validationResult.IsValid = true;
                    validationResult.Message = "Your document is VALID!";
                }



                Trace.WriteLine(DateTime.Now + "-> RETURN validationResult = " + validationResult.IsValid);
                Trace.WriteLine(DateTime.Now + "-> END Generate");

                return(validationResult);
            }
            catch (Exception err)
            {
                Trace.WriteLine(DateTime.Now + "-> ERROR: " + err.Message);
                Trace.WriteLine(DateTime.Now + "-> END Validate");
                return(new EckValidationResult
                {
                    BrokenRules = new List <BrokenRule>(),
                    IsValid = false,
                    Message = "Server internal error!"
                });
            }
        }
예제 #4
0
        public void ASchemaIsValidated()
        {
            //Arrange
            var logProxy     = MockRepository.GenerateStub <ILogProxy>();
            var xmlValidator = new XmlValidator(logProxy);
            var result       = xmlValidator.ValidateXml(
                @"..\..\..\Ratcheter\files\testFile.xml",
                @"..\..\..\Ratcheter\files\testFile.xsd");

            Assert.IsTrue(result);
            logProxy.AssertWasNotCalled(x => x.LogThis(MessageImportance.High, ""));
        }
        public void TestDataElementWithInconsistentChildElementsIsReportedAsAFailure()
        {
            var validator       = new XmlValidator();
            var document        = LoadXmlDocument(@"../../Validator/XmlTestData/InconsistentChildElements.xml");
            var expectedElement = string.Format(Resources.InconsistentElementsMessage,
                                                "2",
                                                "name",
                                                "Wigan");
            var result = validator.ValidateXml(document);

            Assert.IsFalse(result.Item1);
            Assert.AreEqual(expectedElement, result.Item2[0]);
        }
        public static void ValidateXml(string xmlToValidate, IEnumerable <string> schemas, IEnumerable <SchemaReferenceAttribute> schemaReferenceAttributes)
        {
            if (schemas == null)
            {
                throw new ArgumentNullException(nameof(schemas));
            }

            var schemaSet = new XmlSchemaSet();

            foreach (var schema in schemas)
            {
                schemaSet.Add(BizTalkMapSchemaUtility.LoadSchema(schema, schemaReferenceAttributes));
            }

            var xmlValidator = new XmlValidator();

            xmlValidator.ValidateXml(xmlToValidate, schemaSet);
        }
예제 #7
0
        // Event Handler
        public void File_OnChanged(object source, FileSystemEventArgs e)
        {
            //Pause to allow file handle to be cleaned up
            System.Threading.Thread.Sleep(1000);

            string filePath = e.FullPath;

            //*** Validate XML document against schema

            //Get schema document path
            string schemaPath = ConfigurationManager.AppSettings["XmlSchemaFile"];

            if (schemaPath != null)
            {
                //XML doc should be validated against schema
                //Create XmlValidator object
                XmlValidator validator = new XmlValidator();

                //Build schema collection
                XmlSchemaSet schemaCol = new XmlSchemaSet();
                schemaCol.Add(null, schemaPath);

                //Get error log file
                string logFile = ConfigurationManager.AppSettings["XmlValidationErrorLogFile"];
                //Validate Document
                ValidationStatus validationStatus =
                    validator.ValidateXml <string>(filePath,
                                                   schemaCol, null, ((logFile != null)?logFile:null));

                //Check validation status
                if (!validationStatus.Status)
                {
                    //Log that errors occurred during validation and
                    //stop processing on this document
                    this.WriteToLog("Validation of " + filePath + " failed. " +
                                    "See error log file for more details.");
                    File.Move(filePath, filePath + "." + Guid.NewGuid().ToString() + ".notValid");
                    return;
                }
                else                     //Validation successful
                {
                    this.WriteToLog(filePath + " validated successfully. ");
                }
            }
            //Call XML import object to move XML into db
            SQLGenerator gen  = new SQLGenerator();
            SQLInfo      info = gen.CreateSQLStatement(filePath);

            if (info.Status)
            {
                WriteToLog(filePath + " parsed successfully!");

                //Execute SQL statement against database
                SQLInfo dbInfo = gen.ExecuteNonQuery(info.SQL);
                if (dbInfo.Status)
                {
                    WriteToLog(filePath + " data updated successfully in database!");
                }
                else
                {
                    WriteToLog(filePath + " not updated in db successfully. Error: " +
                               dbInfo.StatusMessage);
                }
            }
            else
            {
                WriteToLog(filePath + " not parsed successfully. Error: " +
                           info.StatusMessage);
            }

            //Rename the file
            File.Move(filePath, filePath + "." + Guid.NewGuid().ToString() + ".old");
        }
예제 #8
0
        public void Validate_Invalid_Catalog()
        {
            var result = XmlValidator.ValidateXml(PathHelper.InvalidXmlPath, PathHelper.BooksXsdPath);

            Assert.IsFalse(result);
        }