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 simu开发者_StackOverflow社区late 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.
The problem is XML namespaces.
In your XSD, you define the targetNamespace=
and xmlns=
to both be "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 any 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>
So basically, that XSD isn't validating this XML at all.
You need to either remove those namespaces from your XSD:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
or alternatively, add the default XML namespace (with no prefix) defined in your XSD to your 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 your XSD, those have to be present in the XML as well - and vice versa.
Once you do one or the other solution, you should get a validation error something like:
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.
I am hoping I can help you here. I had a similar issue (with Xml false-positives).
I found two errors in my particular situation, one which may actually be relevant. You must opt-in to various Xml validation via XmlReaderSettings
. Here is a simple usage (taken from 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 ()) { }
精彩评论