Click here to Skip to main content
15,881,139 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I am trying to validate XML (a.xml):
XML
<computer type="s" xmlns="http://aaa.aa1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> </computer>

by schema(a.xsd):
XML
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://aaa.aa"> <xs:element name="computer"> </xs:element> </xs:schema>


This files are validated in .net framework (code bellow) only with warnings instead errors. I tried Stylus, Altova, Java and some online validators and all of them returned validation errors. Only .net framework sample bellow returns warnings. It is a problem because when i decide to suppress warnings and evaluate only errors, the XML is validated successfully, but it's not valid by used schema. (targetNamespace in XSD is different to xnlns in xml (by w3c it is a error)).

Program output:
Warning: Could not find schema information for the element 'http://aaa.aa1:computer'.
Warning: Could not find schema information for the attribute 'type'.
Validation complete

Program output with suppressed warnings:
(remove flag: xmlReadSettings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;)
Walidation complette

What I have tried:

private static void ValidateXmlBySchema(string xmlData, string xsdSchema)
        {
            StringReader xmlDataReader = new StringReader(xmlData);
            StringReader schemaReader = new StringReader(xsdSchema);
            XmlSchema schema = XmlSchema.Read(schemaReader, dataValidationEventHandler);
            XmlReaderSettings xmlReadSettings = new XmlReaderSettings();
            xmlReadSettings.ValidationType = ValidationType.Schema;
            xmlReadSettings.ValidationEventHandler += new ValidationEventHandler(dataValidationEventHandler);
            xmlReadSettings.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation;
//            xmlReadSettings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
            xmlReadSettings.Schemas.Add(schema);
            XmlReader dataReader = XmlReader.Create(xmlDataReader, xmlReadSettings);
            while (dataReader.Read())
            {
            }
            dataReader.Close();
            Console.WriteLine($"Walidation complette");
        }
        static void dataValidationEventHandler(object sender, ValidationEventArgs e)
        {
            Console.WriteLine($"{e.Severity}: {e.Message}");
        }
C#

Posted
Updated 19-Mar-18 3:06am
v3

1 solution

Does this help at all?

Schema Cache[^]
 
Share this answer
 
Comments
Peter Lucansky 20-Mar-18 4:51am    
This, problem isn't about schema cache/ location. I have XML and I try to validate it by XSD. If XSD.TargetNamespace is different to XML.NSURI, for .net framework it's only warning (instead of other platforms (Java, some XML editors...), where i get error). I thing, this bug in .net framework. It's possible to validate couple (xml+ xsd) from my sample with errors in .net?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900