I am developing a system that will receive a XML (XmlDocument) via webservice. I won't have this XML (XmlDocument) on hardisk. It will be managed on memory.
I have a file XSD to validate the XML (XmlDocument) that I receive from my WebService. I am trying to do a example to how validate 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>
also I have my 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 I've put as int, just to simulate the error.
Well, to try 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);
}
On click 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 are a string value in that field and I am not getting error.