Hello, I am trying to validate XML (a.xml):
<computer type="s" xmlns="http://aaa.aa1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> </computer>
by schema(a.xsd):
<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.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}");
}