EDN Admin
Well-known member
I am trying to use the XMLReader to validate xml data with an xsd file. The method runs without error but the validation does not happen. The following is the code I am running and the xml and xsd files I am testing with. I change an element
name in the data for test purposes but a validation event is not raised. If the xml data is changed so that the xml is not well formed I get a system exception raised. So it appears that the validation using the xsd does not work.
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <font face="Consolas" size="2" color="#0000ff" style="font-family:Consolas; color:#0000ff; font-size:x-small
using
</font>
<span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; font-size:x-small System;
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <font face="Consolas" size="2" color="#0000ff" style="font-family:Consolas; color:#0000ff; font-size:x-small
using
</font>
<span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; font-size:x-small System.Collections;
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <font face="Consolas" size="2" color="#0000ff" style="font-family:Consolas; color:#0000ff; font-size:x-small
using
</font>
<span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; font-size:x-small System.Data;
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <font face="Consolas" size="2" color="#0000ff" style="font-family:Consolas; color:#0000ff; font-size:x-small
using
</font>
<span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; font-size:x-small System.IO;
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <font face="Consolas" size="2" color="#0000ff" style="font-family:Consolas; color:#0000ff; font-size:x-small
using
</font>
<span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; font-size:x-small System.Xml;
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <font face="Consolas" size="2" color="#0000ff" style="font-family:Consolas; color:#0000ff; font-size:x-small
using
</font>
<span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; font-size:x-small System.Xml.Schema;
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <font face="Consolas" size="2" color="#0000ff" style="font-family:Consolas; color:#0000ff; font-size:x-small
using
</font>
<span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; font-size:x-small System.Text;
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <font face="Consolas" size="2" color="#0000ff" style="font-family:Consolas; color:#0000ff; font-size:x-small
namespace
</font>
<span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; font-size:x-small XMLValidationProviderClassLibrary
{
<font face="Consolas" size="2" style="font-family:Consolas; font-size:x-small
</font>
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small public<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small class<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; color:#2b91af; font-size:x-small
<span style="font-family:Consolas; color:#2b91af; font-size:x-small
<span style="font-family:Consolas; color:#2b91af; font-size:x-small XMLValidator
<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small
{
<font face="Consolas" size="2" style="font-family:Consolas; font-size:x-small
</font>
<span style="font-family:Consolas; color:#008000; font-size:x-small
<span style="font-family:Consolas; color:#008000; font-size:x-small
<span style="font-family:Consolas; color:#008000; font-size:x-small // Validation Error Count
<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <font face="Consolas" size="2" style="font-family:Consolas; font-size:x-small
</font>
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small static<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small int<span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; font-size:x-small ErrorsCount = 0;<font face="Consolas" size="2" style="font-family:Consolas; font-size:x-small
</font>
<span style="font-family:Consolas; color:#008000; font-size:x-small
<span style="font-family:Consolas; color:#008000; font-size:x-small
<span style="font-family:Consolas; color:#008000; font-size:x-small // Validation Error Message
<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <font face="Consolas" size="2" style="font-family:Consolas; font-size:x-small
</font>
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small static<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small string<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small
ErrorMessage = <span style="font-family:Consolas; color:#a31515; font-size:x-small <span style="font-family:Consolas; color:#a31515; font-size:x-small <span style="font-family:Consolas; color:#a31515; font-size:x-small ""<span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; font-size:x-small ;<font face="Consolas" size="2" style="font-family:Consolas; font-size:x-small
</font>
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small public<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small static<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small void<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small
ValidationHandler(<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small object<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small
sender, <span style="font-family:Consolas; color:#2b91af; font-size:x-small <span style="font-family:Consolas; color:#2b91af; font-size:x-small <span style="font-family:Consolas; color:#2b91af; font-size:x-small ValidationEventArgs<span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; font-size:x-small args)
{
<font face="Consolas" size="2" style="font-family:Consolas; font-size:x-small
ErrorMessage = ErrorMessage + args.Message +
</font>
<span style="font-family:Consolas; color:#a31515; font-size:x-small <span style="font-family:Consolas; color:#a31515; font-size:x-small <span style="font-family:Consolas; color:#a31515; font-size:x-small "rn"<span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; font-size:x-small ;
ErrorsCount++;
}
<font face="Consolas" size="2" style="font-family:Consolas; font-size:x-small
</font>
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small public<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small string<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small
Validate(<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small string<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small
XMLPath, <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small string<span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; font-size:x-small XSDPath)
{
<font face="Consolas" size="2" style="font-family:Consolas; font-size:x-small
</font>
<span style="font-family:Consolas; color:#0000ff; font-size:x-small
<span style="font-family:Consolas; color:#0000ff; font-size:x-small
<span style="font-family:Consolas; color:#0000ff; font-size:x-small try
<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small
{
<font face="Consolas" size="2" style="font-family:Consolas; font-size:x-small
</font>
<span style="font-family:Consolas; color:#2b91af; font-size:x-small <span style="font-family:Consolas; color:#2b91af; font-size:x-small <span style="font-family:Consolas; color:#2b91af; font-size:x-small XmlReaderSettings<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small
objSettings = <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small new<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; color:#2b91af; font-size:x-small <span style="font-family:Consolas; color:#2b91af; font-size:x-small <span style="font-family:Consolas; color:#2b91af; font-size:x-small XmlReaderSettings<span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; font-size:x-small ();<font face="Consolas" size="2" style="font-family:Consolas; font-size:x-small
objSettings.Schemas.Add(
</font>
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small null<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small ,
<span style="font-family:Consolas; color:#2b91af; font-size:x-small <span style="font-family:Consolas; color:#2b91af; font-size:x-small <span style="font-family:Consolas; color:#2b91af; font-size:x-small XmlReader<span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; font-size:x-small .Create(XSDPath));<font face="Consolas" size="2" style="font-family:Consolas; font-size:x-small
objSettings.ValidationFlags =
</font>
<span style="font-family:Consolas; color:#2b91af; font-size:x-small <span style="font-family:Consolas; color:#2b91af; font-size:x-small <span style="font-family:Consolas; color:#2b91af; font-size:x-small XmlSchemaValidationFlags<span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; font-size:x-small .ReportValidationWarnings;<font face="Consolas" size="2" style="font-family:Consolas; font-size:x-small
objSettings.ValidationEventHandler +=
</font>
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small new<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; color:#2b91af; font-size:x-small <span style="font-family:Consolas; color:#2b91af; font-size:x-small <span style="font-family:Consolas; color:#2b91af; font-size:x-small ValidationEventHandler<span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; font-size:x-small (ValidationHandler);<font face="Consolas" size="2" style="font-family:Consolas; font-size:x-small
objSettings.IgnoreComments =
</font>
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small true<span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; font-size:x-small ;<font face="Consolas" size="2" style="font-family:Consolas; font-size:x-small
objSettings.IgnoreWhitespace =
</font>
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small true<span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; font-size:x-small ;<font face="Consolas" size="2" style="font-family:Consolas; font-size:x-small
</font>
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small using<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small
(<span style="font-family:Consolas; color:#2b91af; font-size:x-small <span style="font-family:Consolas; color:#2b91af; font-size:x-small <span style="font-family:Consolas; color:#2b91af; font-size:x-small XmlReader<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small
myReader = <span style="font-family:Consolas; color:#2b91af; font-size:x-small <span style="font-family:Consolas; color:#2b91af; font-size:x-small <span style="font-family:Consolas; color:#2b91af; font-size:x-small XmlReader<span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; font-size:x-small .Create(XMLPath, objSettings))
{
<font face="Consolas" size="2" style="font-family:Consolas; font-size:x-small
</font>
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small while<span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; font-size:x-small (myReader.Read());
myReader.Close();
}
<font face="Consolas" size="2" style="font-family:Consolas; font-size:x-small
</font>
<span style="font-family:Consolas; color:#008000; font-size:x-small
<span style="font-family:Consolas; color:#008000; font-size:x-small
<span style="font-family:Consolas; color:#008000; font-size:x-small // Raise exception, if XML validation fails
<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <font face="Consolas" size="2" style="font-family:Consolas; font-size:x-small
</font>
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small if<span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; font-size:x-small (ErrorsCount > 0)
{
<font face="Consolas" size="2" style="font-family:Consolas; font-size:x-small
</font>
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small return<span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; font-size:x-small ErrorMessage;
}
<font face="Consolas" size="2" style="font-family:Consolas; font-size:x-small
</font>
<span style="font-family:Consolas; color:#0000ff; font-size:x-small
<span style="font-family:Consolas; color:#0000ff; font-size:x-small
<span style="font-family:Consolas; color:#0000ff; font-size:x-small else
<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small
{
<font face="Consolas" size="2" style="font-family:Consolas; font-size:x-small
</font>
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small return<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; color:#a31515; font-size:x-small <span style="font-family:Consolas; color:#a31515; font-size:x-small <span style="font-family:Consolas; color:#a31515; font-size:x-small "XML validation succeeded."<span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; font-size:x-small ;
}
}
<font face="Consolas" size="2" style="font-family:Consolas; font-size:x-small
</font>
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small catch<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small
(<span style="font-family:Consolas; color:#2b91af; font-size:x-small <span style="font-family:Consolas; color:#2b91af; font-size:x-small <span style="font-family:Consolas; color:#2b91af; font-size:x-small Exception<span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; font-size:x-small error)
{
<font face="Consolas" size="2" style="font-family:Consolas; font-size:x-small
</font>
<span style="font-family:Consolas; color:#008000; font-size:x-small
<span style="font-family:Consolas; color:#008000; font-size:x-small
<span style="font-family:Consolas; color:#008000; font-size:x-small // XML Validation errored
<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <font face="Consolas" size="2" style="font-family:Consolas; font-size:x-small
</font>
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small string<span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; font-size:x-small errMsg = error.ToString();<font face="Consolas" size="2" style="font-family:Consolas; font-size:x-small
</font>
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small return<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small
errMsg + <span style="font-family:Consolas; color:#a31515; font-size:x-small <span style="font-family:Consolas; color:#a31515; font-size:x-small <span style="font-family:Consolas; color:#a31515; font-size:x-small "rn"<span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; font-size:x-small + ErrorMessage;
}
}
}
}
<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small Product.xml
<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <?xml
version="1.0" encoding="utf-8"?><br/>
<Product ProductID="123 <br/>
<ProductName>Rugby jersey</ProductName><br/>
</Product>
<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small Product.xsd
<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <?xml
version="1.0"?><br/>
<xsd:schema xmlns:xsd=" http://www.w3.org/2001/XMLSchema http://www.w3.org/2001/XMLSchema <br/>
<xsd:element name="Product <br/>
<xsd:complexType><br/>
<xsd:sequence><br/>
<xsd:element name="ProductName" type="xsd:string"/><br/>
</xsd:sequence><br/>
<xsd:attribute name="ProductID" use="required" type="xsd:int"/><br/>
</xsd:complexType><br/>
</xsd:element><br/>
</xsd:schema>
<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small I
have tried several different xml and xsd samples with the same result.
<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small What
is wrong here?
<font face="Consolas" size="2 <font face="Consolas" size="2 <font face="Consolas" size="2 <font face="Consolas" size="2 <font face="Consolas" size="2 <font face="Consolas" size="2 Thanks.</font></font></font></font></font></font>
View the full article
name in the data for test purposes but a validation event is not raised. If the xml data is changed so that the xml is not well formed I get a system exception raised. So it appears that the validation using the xsd does not work.
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <font face="Consolas" size="2" color="#0000ff" style="font-family:Consolas; color:#0000ff; font-size:x-small
using
</font>
<span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; font-size:x-small System;
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <font face="Consolas" size="2" color="#0000ff" style="font-family:Consolas; color:#0000ff; font-size:x-small
using
</font>
<span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; font-size:x-small System.Collections;
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <font face="Consolas" size="2" color="#0000ff" style="font-family:Consolas; color:#0000ff; font-size:x-small
using
</font>
<span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; font-size:x-small System.Data;
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <font face="Consolas" size="2" color="#0000ff" style="font-family:Consolas; color:#0000ff; font-size:x-small
using
</font>
<span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; font-size:x-small System.IO;
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <font face="Consolas" size="2" color="#0000ff" style="font-family:Consolas; color:#0000ff; font-size:x-small
using
</font>
<span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; font-size:x-small System.Xml;
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <font face="Consolas" size="2" color="#0000ff" style="font-family:Consolas; color:#0000ff; font-size:x-small
using
</font>
<span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; font-size:x-small System.Xml.Schema;
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <font face="Consolas" size="2" color="#0000ff" style="font-family:Consolas; color:#0000ff; font-size:x-small
using
</font>
<span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; font-size:x-small System.Text;
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <font face="Consolas" size="2" color="#0000ff" style="font-family:Consolas; color:#0000ff; font-size:x-small
namespace
</font>
<span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; font-size:x-small XMLValidationProviderClassLibrary
{
<font face="Consolas" size="2" style="font-family:Consolas; font-size:x-small
</font>
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small public<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small class<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; color:#2b91af; font-size:x-small
<span style="font-family:Consolas; color:#2b91af; font-size:x-small
<span style="font-family:Consolas; color:#2b91af; font-size:x-small XMLValidator
<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small
{
<font face="Consolas" size="2" style="font-family:Consolas; font-size:x-small
</font>
<span style="font-family:Consolas; color:#008000; font-size:x-small
<span style="font-family:Consolas; color:#008000; font-size:x-small
<span style="font-family:Consolas; color:#008000; font-size:x-small // Validation Error Count
<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <font face="Consolas" size="2" style="font-family:Consolas; font-size:x-small
</font>
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small static<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small int<span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; font-size:x-small ErrorsCount = 0;<font face="Consolas" size="2" style="font-family:Consolas; font-size:x-small
</font>
<span style="font-family:Consolas; color:#008000; font-size:x-small
<span style="font-family:Consolas; color:#008000; font-size:x-small
<span style="font-family:Consolas; color:#008000; font-size:x-small // Validation Error Message
<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <font face="Consolas" size="2" style="font-family:Consolas; font-size:x-small
</font>
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small static<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small string<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small
ErrorMessage = <span style="font-family:Consolas; color:#a31515; font-size:x-small <span style="font-family:Consolas; color:#a31515; font-size:x-small <span style="font-family:Consolas; color:#a31515; font-size:x-small ""<span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; font-size:x-small ;<font face="Consolas" size="2" style="font-family:Consolas; font-size:x-small
</font>
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small public<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small static<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small void<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small
ValidationHandler(<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small object<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small
sender, <span style="font-family:Consolas; color:#2b91af; font-size:x-small <span style="font-family:Consolas; color:#2b91af; font-size:x-small <span style="font-family:Consolas; color:#2b91af; font-size:x-small ValidationEventArgs<span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; font-size:x-small args)
{
<font face="Consolas" size="2" style="font-family:Consolas; font-size:x-small
ErrorMessage = ErrorMessage + args.Message +
</font>
<span style="font-family:Consolas; color:#a31515; font-size:x-small <span style="font-family:Consolas; color:#a31515; font-size:x-small <span style="font-family:Consolas; color:#a31515; font-size:x-small "rn"<span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; font-size:x-small ;
ErrorsCount++;
}
<font face="Consolas" size="2" style="font-family:Consolas; font-size:x-small
</font>
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small public<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small string<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small
Validate(<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small string<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small
XMLPath, <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small string<span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; font-size:x-small XSDPath)
{
<font face="Consolas" size="2" style="font-family:Consolas; font-size:x-small
</font>
<span style="font-family:Consolas; color:#0000ff; font-size:x-small
<span style="font-family:Consolas; color:#0000ff; font-size:x-small
<span style="font-family:Consolas; color:#0000ff; font-size:x-small try
<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small
{
<font face="Consolas" size="2" style="font-family:Consolas; font-size:x-small
</font>
<span style="font-family:Consolas; color:#2b91af; font-size:x-small <span style="font-family:Consolas; color:#2b91af; font-size:x-small <span style="font-family:Consolas; color:#2b91af; font-size:x-small XmlReaderSettings<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small
objSettings = <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small new<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; color:#2b91af; font-size:x-small <span style="font-family:Consolas; color:#2b91af; font-size:x-small <span style="font-family:Consolas; color:#2b91af; font-size:x-small XmlReaderSettings<span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; font-size:x-small ();<font face="Consolas" size="2" style="font-family:Consolas; font-size:x-small
objSettings.Schemas.Add(
</font>
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small null<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small ,
<span style="font-family:Consolas; color:#2b91af; font-size:x-small <span style="font-family:Consolas; color:#2b91af; font-size:x-small <span style="font-family:Consolas; color:#2b91af; font-size:x-small XmlReader<span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; font-size:x-small .Create(XSDPath));<font face="Consolas" size="2" style="font-family:Consolas; font-size:x-small
objSettings.ValidationFlags =
</font>
<span style="font-family:Consolas; color:#2b91af; font-size:x-small <span style="font-family:Consolas; color:#2b91af; font-size:x-small <span style="font-family:Consolas; color:#2b91af; font-size:x-small XmlSchemaValidationFlags<span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; font-size:x-small .ReportValidationWarnings;<font face="Consolas" size="2" style="font-family:Consolas; font-size:x-small
objSettings.ValidationEventHandler +=
</font>
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small new<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; color:#2b91af; font-size:x-small <span style="font-family:Consolas; color:#2b91af; font-size:x-small <span style="font-family:Consolas; color:#2b91af; font-size:x-small ValidationEventHandler<span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; font-size:x-small (ValidationHandler);<font face="Consolas" size="2" style="font-family:Consolas; font-size:x-small
objSettings.IgnoreComments =
</font>
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small true<span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; font-size:x-small ;<font face="Consolas" size="2" style="font-family:Consolas; font-size:x-small
objSettings.IgnoreWhitespace =
</font>
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small true<span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; font-size:x-small ;<font face="Consolas" size="2" style="font-family:Consolas; font-size:x-small
</font>
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small using<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small
(<span style="font-family:Consolas; color:#2b91af; font-size:x-small <span style="font-family:Consolas; color:#2b91af; font-size:x-small <span style="font-family:Consolas; color:#2b91af; font-size:x-small XmlReader<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small
myReader = <span style="font-family:Consolas; color:#2b91af; font-size:x-small <span style="font-family:Consolas; color:#2b91af; font-size:x-small <span style="font-family:Consolas; color:#2b91af; font-size:x-small XmlReader<span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; font-size:x-small .Create(XMLPath, objSettings))
{
<font face="Consolas" size="2" style="font-family:Consolas; font-size:x-small
</font>
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small while<span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; font-size:x-small (myReader.Read());
myReader.Close();
}
<font face="Consolas" size="2" style="font-family:Consolas; font-size:x-small
</font>
<span style="font-family:Consolas; color:#008000; font-size:x-small
<span style="font-family:Consolas; color:#008000; font-size:x-small
<span style="font-family:Consolas; color:#008000; font-size:x-small // Raise exception, if XML validation fails
<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <font face="Consolas" size="2" style="font-family:Consolas; font-size:x-small
</font>
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small if<span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; font-size:x-small (ErrorsCount > 0)
{
<font face="Consolas" size="2" style="font-family:Consolas; font-size:x-small
</font>
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small return<span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; font-size:x-small ErrorMessage;
}
<font face="Consolas" size="2" style="font-family:Consolas; font-size:x-small
</font>
<span style="font-family:Consolas; color:#0000ff; font-size:x-small
<span style="font-family:Consolas; color:#0000ff; font-size:x-small
<span style="font-family:Consolas; color:#0000ff; font-size:x-small else
<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small
{
<font face="Consolas" size="2" style="font-family:Consolas; font-size:x-small
</font>
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small return<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; color:#a31515; font-size:x-small <span style="font-family:Consolas; color:#a31515; font-size:x-small <span style="font-family:Consolas; color:#a31515; font-size:x-small "XML validation succeeded."<span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; font-size:x-small ;
}
}
<font face="Consolas" size="2" style="font-family:Consolas; font-size:x-small
</font>
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small catch<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small
(<span style="font-family:Consolas; color:#2b91af; font-size:x-small <span style="font-family:Consolas; color:#2b91af; font-size:x-small <span style="font-family:Consolas; color:#2b91af; font-size:x-small Exception<span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; font-size:x-small error)
{
<font face="Consolas" size="2" style="font-family:Consolas; font-size:x-small
</font>
<span style="font-family:Consolas; color:#008000; font-size:x-small
<span style="font-family:Consolas; color:#008000; font-size:x-small
<span style="font-family:Consolas; color:#008000; font-size:x-small // XML Validation errored
<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <font face="Consolas" size="2" style="font-family:Consolas; font-size:x-small
</font>
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small string<span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; font-size:x-small errMsg = error.ToString();<font face="Consolas" size="2" style="font-family:Consolas; font-size:x-small
</font>
<span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small <span style="font-family:Consolas; color:#0000ff; font-size:x-small return<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small
errMsg + <span style="font-family:Consolas; color:#a31515; font-size:x-small <span style="font-family:Consolas; color:#a31515; font-size:x-small <span style="font-family:Consolas; color:#a31515; font-size:x-small "rn"<span style="font-family:Consolas; font-size:x-small
<span style="font-family:Consolas; font-size:x-small + ErrorMessage;
}
}
}
}
<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small Product.xml
<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <?xml
version="1.0" encoding="utf-8"?><br/>
<Product ProductID="123 <br/>
<ProductName>Rugby jersey</ProductName><br/>
</Product>
<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small Product.xsd
<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <?xml
version="1.0"?><br/>
<xsd:schema xmlns:xsd=" http://www.w3.org/2001/XMLSchema http://www.w3.org/2001/XMLSchema <br/>
<xsd:element name="Product <br/>
<xsd:complexType><br/>
<xsd:sequence><br/>
<xsd:element name="ProductName" type="xsd:string"/><br/>
</xsd:sequence><br/>
<xsd:attribute name="ProductID" use="required" type="xsd:int"/><br/>
</xsd:complexType><br/>
</xsd:element><br/>
</xsd:schema>
<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small I
have tried several different xml and xsd samples with the same result.
<span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small <span style="font-family:Consolas; font-size:x-small What
is wrong here?
<font face="Consolas" size="2 <font face="Consolas" size="2 <font face="Consolas" size="2 <font face="Consolas" size="2 <font face="Consolas" size="2 <font face="Consolas" size="2 Thanks.</font></font></font></font></font></font>
View the full article