Validating XmlDocument with XSD

I am developing a system that will receive XML (XmlDocument) through webservice. I will not have this XML (XmlDocument) on the hard drive. It will be controlled in memory.

I have an XML Validation XSD (XmlDocument) file that I get from my WebService. I am trying to make an example of how to check this Xml.

My XML:

<?xml version="1.0"?> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> 

I also have an XSD:

 <?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.w3schools.com" xmlns="http://www.w3schools.com" elementFormDefault="qualified"> <xs:element name="note"> <xs:complexType> <xs:sequence> <xs:element name="to" type="xs:string"/> <xs:element name="from" type="xs:string"/> <xs:element name="heading" type="xs:string"/> <xs:element name="body" type="xs:int"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> 

As we can see, the body field, which I set as an int , is just to simulate an error.

Ok, to try to get the error, I have the following code:

 //event handler to manage the errors private static void verifyErrors(object sender, ValidationEventArgs args) { if (args.Severity == XmlSeverityType.Warning) MessageBox.Show(args.Message); } 

When I click the button, I have:

  private void button1_Click(object sender, EventArgs e) { try { // my XmlDocument (in this case I will load from hardisk) XmlDocument xml = new XmlDocument(); // load the XSD schema.. is this right? xml.Schemas.Add("http://www.w3schools.com", "meuEsquema.xsd"); // Load my XML from hardisk xml.Load("meusDados.xml"); // event handler to manage the errors from XmlDocument object ValidationEventHandler veh = new ValidationEventHandler(verificaErros); // try to validate my XML.. and the event handler verifyError will show the error xml.Validate(veh); } catch { // do nothing.. just to test } } 

The problem is that I changed the body field to int , but there is a string value in this field, and I am not getting an error.

+4
source share
2 answers

The problem is XML namespaces.

In your XSD, you define targetNamespace= and xmlns= as "http://www.w3schools.com" :

 <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.w3schools.com" xmlns="http://www.w3schools.com" elementFormDefault="qualified"> 

However, your XML document does not contain XML namespaces.

 <?xml version="1.0"?> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> 

Basically, XSD does not validate this XML at all .

You need to either remove these namespaces from your XSD:

 <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> 

or, conversely, add the default XML namespace (no prefix) defined in XSD to XML:

 <?xml version="1.0"?> <note xmlns="http://www.w3schools.com"> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> 

If you have XML namespaces in XSD, they must also be present in XML - and vice versa.

As soon as you make one or the other decision, you should get a validation error, for example:

 Validation error: The 'body' element is invalid - The value 'Don't forget me this weekend!' is invalid according to its datatype 'http://www.w3.org/2001/XMLSchema:int' - The string 'Don't forget me this weekend!' is not a valid Int32 value. 
+5
source

I hope I can help you here. I had a similar problem (with Xml false positives).

In my specific situation, I found two errors that can really make a difference. You must refuse Xml validation using XmlReaderSettings . Here is a simple use (taken from the post above)

 string schemaFileName = @"sampleSchema.xsd"; string xmlFileName = @"sampleXml.xml"; XmlReaderSettings settings = new XmlReaderSettings { ValidationType = ValidationType.Schema, ValidationFlags = XmlSchemaValidationFlags.ProcessInlineSchema | XmlSchemaValidationFlags.ProcessSchemaLocation | XmlSchemaValidationFlags.ReportValidationWarnings, }; settings.Schemas.Add (schema); settings.ValidationEventHandler += (o, e) => { throw new Exception("CRASH"); }; XmlSchema schema = XmlSchema.Read ( File.OpenText (schemaFileName), (o, e) => { throw new Exception ("BOOM"); }); // obviously you don't need to read Xml from file, // just skip the load bit and supply raw DOM object XmlReader reader = XmlReader.Create (xmlFileName, settings); while (reader.Read ()) { } 
0
source

Source: https://habr.com/ru/post/1316466/


All Articles