public void ShouldFailToValidateXsdSchema()
        {
            // Arrange
            string xmlContent = XmlTestData.GetInvalidXmlContent();
            string xsdContent = XmlTestData.GetXsdMarkup();

            IXsdValidator xsdValidator = XsdValidator.Instance;

            // Act
            var validationResult = xsdValidator.Validate(xmlContent, xsdContent);

            // Assert
            Assert.That(validationResult.IsValid, Is.False);
            Assert.That(validationResult.Errors, Has.Count.EqualTo(1));
            // var meg = validationResult.Errors.ElementAt(0).Message;
        }
Exemplo n.º 2
0
        public void ShouldFailToValidateXsdSchema()
        {
            // Arrange
            string xmlContent = XmlTestData.GetInvalidXmlContent();
            string xsdContent = XmlTestData.GetXsdMarkup();

            IXsdValidator xsdValidator = new XsdValidator();

            // Act
            var validationResult = xsdValidator.Validate(xmlContent, xsdContent);

            // Assert
            validationResult.IsValid.Should().BeFalse();
            validationResult.Errors.Should().HaveCount(1);
            validationResult.Errors.ElementAt(0).Message.Should().Be("The element 'Root' has invalid child element 'Child3'. List of possible elements expected: 'Child2'.");
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            IXsdValidator xsdValidator = new XsdValidator();
            var           xsdContent   = XmlTestData.GetXsdMarkup();

            {
                Console.WriteLine("// Demo: Validate a valid XML file against an XSD schema");
                string validXmlContent  = XmlTestData.GetValidXmlContent();
                var    validationResult = xsdValidator.Validate(validXmlContent, xsdContent);
                Console.WriteLine($"validationResult.IsValid: {validationResult.IsValid}");
                Console.WriteLine($"validationResult.Errors ({validationResult.Errors.Count()}):\n{FormatValidationErrors(validationResult)}");
                Console.WriteLine();
            }

            {
                Console.WriteLine("// Demo: Validate an invalid XML file against an XSD schema");
                string invalidXmlContent = XmlTestData.GetInvalidXmlContent();
                var    validationResult  = xsdValidator.Validate(invalidXmlContent, xsdContent);
                Console.WriteLine($"validationResult.IsValid: {validationResult.IsValid}");
                Console.WriteLine($"validationResult.Errors ({validationResult.Errors.Count()}):\n{FormatValidationErrors(validationResult)}");
                Console.WriteLine();
            }

            Console.WriteLine("// Demo: Serialize/deserialize string to XML");
            const string inputString = "This is a test string";

            Console.WriteLine($"inputString:\n{inputString}");
            Console.WriteLine();

            var serialized = XmlSerializerHelper.Current.SerializeToXml(inputString);

            Console.WriteLine($"SerializeToXml:\n{serialized}");
            Console.WriteLine();

            var deserialized = XmlSerializerHelper.Current.DeserializeFromXml <string>(serialized);

            Console.WriteLine($"DeserializeFromXml:\n{deserialized}");
            Console.WriteLine();

            Console.ReadKey();
        }