Answered by:
XML validation using XMLReader not working

Question
-
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.
using
System;
using
System.Collections;
using
System.Data;
using
System.IO;
using
System.Xml;
using
System.Xml.Schema;
using
System.Text;
namespace
XMLValidationProviderClassLibrary
{
public class
XMLValidator
{
// Validation Error Count
static int
ErrorsCount = 0;
// Validation Error Message
static string ErrorMessage = ""
;
public static void ValidationHandler(object sender, ValidationEventArgs
args)
{
ErrorMessage = ErrorMessage + args.Message +
"\r\n"
;
ErrorsCount++;
}
public string Validate(string XMLPath, string
XSDPath)
{
try
{
XmlReaderSettings objSettings = new XmlReaderSettings
();
objSettings.Schemas.Add(
null, XmlReader
.Create(XSDPath));
objSettings.ValidationFlags =
XmlSchemaValidationFlags
.ReportValidationWarnings;
objSettings.ValidationEventHandler +=
new ValidationEventHandler
(ValidationHandler);
objSettings.IgnoreComments =
true
;
objSettings.IgnoreWhitespace =
true
;
using (XmlReader myReader = XmlReader
.Create(XMLPath, objSettings))
{
while
(myReader.Read());
myReader.Close();
}
// Raise exception, if XML validation fails
if
(ErrorsCount > 0)
{
return
ErrorMessage;
}
else
{
return "XML validation succeeded."
;
}
}
catch (Exception
error)
{
// XML Validation errored
string
errMsg = error.ToString();
return errMsg + "\r\n"
+ ErrorMessage;
}
}
}
}
Product.xml
<?xml version="1.0" encoding="utf-8"?>
<Product ProductID="123">
<ProductName>Rugby jersey</ProductName>
</Product>Product.xsd
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="Product">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="ProductName" type="xsd:string"/>
</xsd:sequence>
<xsd:attribute name="ProductID" use="required" type="xsd:int"/>
</xsd:complexType>
</xsd:element>
</xsd:schema>I have tried several different xml and xsd samples with the same result.
What is wrong here?
Thanks.
- Moved by Jackie-Sun Friday, July 29, 2011 7:21 AM (From:Visual C# General)
Wednesday, July 27, 2011 1:32 PM
Answers
-
You missed one line: objSettings.ValidationType = ValidationType.Schema;
Set the ValidationEventHandler is not enough, you have to set the ValidationType.
I reformat your code a liitle bit as follow:
try { XmlReaderSettings objSettings = new XmlReaderSettings(); objSettings.Schemas.Add(null, XmlReader.Create("Product.xsd")); objSettings.ValidationType = ValidationType.Schema; objSettings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings; objSettings.ValidationEventHandler += new ValidationEventHandler(ValidationHandler); objSettings.IgnoreComments = true; objSettings.IgnoreWhitespace = true; using (XmlReader myReader = XmlReader.Create("Product.xml", objSettings)) { while (myReader.Read()) ; myReader.Close(); } } catch (Exception error) { // XML Validation errored string errMsg = error.ToString(); Console.WriteLine(errMsg); }
- Proposed as answer by Hubery Yuan Friday, August 19, 2011 9:01 AM
- Marked as answer by Lionello LunesuModerator Monday, September 5, 2011 1:41 AM
Friday, August 19, 2011 9:01 AM
All replies
-
Well,
If you have an .xsd and an .xml file, use the dataset object.
You can use the methods ds.ReadXML and ds.ReadXMLSchema
Adam
Ctrl+ZWednesday, July 27, 2011 1:58 PM -
You missed one line: objSettings.ValidationType = ValidationType.Schema;
Set the ValidationEventHandler is not enough, you have to set the ValidationType.
I reformat your code a liitle bit as follow:
try { XmlReaderSettings objSettings = new XmlReaderSettings(); objSettings.Schemas.Add(null, XmlReader.Create("Product.xsd")); objSettings.ValidationType = ValidationType.Schema; objSettings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings; objSettings.ValidationEventHandler += new ValidationEventHandler(ValidationHandler); objSettings.IgnoreComments = true; objSettings.IgnoreWhitespace = true; using (XmlReader myReader = XmlReader.Create("Product.xml", objSettings)) { while (myReader.Read()) ; myReader.Close(); } } catch (Exception error) { // XML Validation errored string errMsg = error.ToString(); Console.WriteLine(errMsg); }
- Proposed as answer by Hubery Yuan Friday, August 19, 2011 9:01 AM
- Marked as answer by Lionello LunesuModerator Monday, September 5, 2011 1:41 AM
Friday, August 19, 2011 9:01 AM