vihrao Posted February 5, 2010 Posted February 5, 2010 I am using xml validation of a file with schema. I cannot catch mismatch between open and close tags. Here is the c# code: XmlSchema xmlSchema = XmlSchema.Read(new XmlTextReader("XMLSchema1.xsd"), new ValidationEventHandler(booksSettingsValidationEventHandler)); XmlReaderSettings xmlReaderSettings = new XmlReaderSettings(); xmlReaderSettings.ValidationType = ValidationType.Schema; xmlReaderSettings.Schemas.Add(xmlSchema); xmlReaderSettings.ValidationEventHandler += new ValidationEventHandler(booksSettingsValidationEventHandler); using (XmlReader xmlReader = XmlReader.Create(new XmlTextReader("XMLFile1.xml"), xmlReaderSettings)) while (xmlReader.Read()) ; Here is the validator: static void booksSettingsValidationEventHandler(object sender, ValidationEventArgs e) { if (e.Severity == XmlSeverityType.Warning) { Console.Write("WARNING: "); Console.WriteLine(e.Message); } else if (e.Severity == XmlSeverityType.Error) { Console.Write("ERROR: "); Console.WriteLine(e.Message); } } Here is the XMLSchema1.xsd schema: <?xml version="1.0" encoding="utf-8" ?> <!--Created with Liquid XML Studio Developer Edition (Trial) (http://www.liquid-technologies.com)--> <xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.contoso.com/books" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="bookstore"> <xs:complexType> <xs:sequence> <xs:element maxOccurs="unbounded" name="book"> <xs:complexType> <xs:sequence> <xs:element name="title" type="xs:string" /> <xs:element name="author"> <xs:complexType> <xs:sequence> <xs:element minOccurs="0" name="name" type="xs:string" /> <xs:element minOccurs="0" name="first-name" type="xs:string" /> <xs:element minOccurs="0" name="last-name" type="xs:string" /> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="price" type="xs:decimal" /> </xs:sequence> <xs:attribute name="genre" type="xs:string" use="required" /> <xs:attribute name="publicationdate" type="xs:unsignedShort" use="required" /> <xs:attribute name="ISBN" type="xs:string" use="required" /> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> Here is the XMLFile1.xml file: <?xml version="1.0" encoding="utf-8" ?> <bookstore xmlns="http://www.contoso.com/books"> <book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0"> <title>The Autobiography of Benjamin Franklin</title> <author> <first-name>Benjamin</first-name> <last-name>Franklin</last-name> </author> <price>8.99</price> </book> <book genre="novel" publicationdate="1967" ISBN="0-201-63361-2"> <title>The Confidence Man</title> <author> <first-name>Herman</first-name> <last-name>Melville</last-name> </author> <price>11.99</price> </book> <book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6"> <title>The Gorgias</title> <author> <name>Plato</name> </author> <price>9.99</price> </book> </bookstore> If I change one of the xml file elements from first-name to fname, the validator cataches the error. However, I keep the opening tag in xml file as <book> but change the closing tag from </book> to </bk> The validator is never fired. Instead it crashes with exception in while (xmlReader.Read()) ; Can someone please tell me how to catch mismatch in closing and opening tags. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.