[Mono-list] Validating XML
Aquil H. Abdullah
aquil.abdullah at gmail.com
Wed Apr 16 20:07:10 EDT 2008
I am working with XML and I looked at the C# Cookbook to see how to validate
XML and I ran across something similar to the following code:
using System;
using System.IO;
using System.Xml;
using System.Xml.Schema;
namespace Etro.RSchema2{
public class RSchema2{
public static void ValidateXml(string xmlFilename, string
validatorFilename,
ValidationType validationType){
// Create a settings object for XML Document
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationEventHandler += MyValidationEventHandler;
settings.ValidationType = validationType;
settings.Schemas.Add(null, validatorFilename);
// Open the bookbad.xml file.
if (settings.Schemas.Count > 0){
using (XmlReader reader = XmlReader.Create(xmlFilename, settings)){
// Replace validReader with reader for the whole loop.
while (reader.Read()){
if (reader.NodeType == XmlNodeType.Element){
Console.Write("<{0}", reader.Name);
while (reader.MoveToNextAttribute()) {
Console.Write(" {0}='{1}'", reader.Name,
reader.Value);
}
Console.Write(">");
}
else if (reader.NodeType == XmlNodeType.Text){
Console.Write(reader.Value);
}else if (reader.NodeType == XmlNodeType.EndElement){
Console.WriteLine("</{0}>", reader.Name);
}
}
}
}
}
private static void MyValidationEventHandler(object sender,
ValidationEventArgs e){
Console.WriteLine("Validation Error Message: {0}", e.Message);
Console.WriteLine("Validation Error Severity: {0}", e.Severity);
if (e.Exception != null)
{
Console.WriteLine("Validation Error Line Number: {0}",
e.Exception.LineNumber);
Console.WriteLine("Validation Error Line Position: {0}",
e.Exception.LinePosition);
Console.WriteLine("Validation Error Source: {0}",
e.Exception.Source);
Console.WriteLine("Validation Error Source Schema: {0}",
e.Exception.SourceSchemaObject);
Console.WriteLine("Validation Error Source Uri: {0}",
e.Exception.SourceUri);
Console.WriteLine("Validation Error thrown from: {0}",
e.Exception.TargetSite);
Console.WriteLine("Validation Error callstack: {0}",
e.Exception.StackTrace);
}
}
public static void Main(string[] args){
// Check args
if (args.Length < 2 || args.Length > 3){
PrintUsage();
}
string xmlFile = string.Copy(args[0]);
string validatorFile = string.Copy(args[1]);
ValidationType validationType = ValidationType.Schema;
// Check Validation type of Validator File
if (args.Length == 3){
if (args[2] == "schema"){
validationType = ValidationType.Schema;
}else if (args[2] == "dtd"){
validationType = ValidationType.DTD;
}
}
// Check that files exist
CheckFile(xmlFile);
CheckFile(validatorFile);
ValidateXml(xmlFile, validatorFile, validationType);
}
// Utility Methods
// Print Application Usage
static void PrintUsage(){
Console.WriteLine("usage:\n\tmono rschema1 xmlfile xsdfile");
Environment.Exit(1);
}
// Check if file exists
static void CheckFile(string fileName){
if (!File.Exists(fileName)){
Console.WriteLine("Could not locate {0}", fileName);
Environment.Exit(1);
}
}
}
}
When I run this code with a simple example such as note.xml and note.xsd
note.xml
-----------
<?xml version="1.0"?>
<note xmlns="file://home/aha/devel/cs/nshell/XmlSchemaReader">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend</body>
<boo>man</boo>
</note>
note.xsd
------------
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="file://home/aha/devel/cs/nshell/XmlSchemaReader"
xmlns="file://home/aha/devel/cs/nshell/XmlSchemaReader"
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:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
I get the following output:
aha at pi:~/devel/cs/nshell/XmlSchemaReader$ mono rschema2.exe note.xml
note.xsd
<note xmlns='file://home/aha/devel/cs/nshell/XmlSchemaReader'><to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend</body>
Validation Error Message: XmlSchema error: Invalid start element:
file://home/aha/devel/cs/nshell/XmlSchemaReader:boo XML Line 7, Position 4.
Validation Error Severity: Error
Validation Error Line Number: 7
Validation Error Line Position: 4
Validation Error Source:
Validation Error Source Schema:
Validation Error Source Uri:
Validation Error thrown from:
Validation Error callstack:
Validation Error Message: XmlSchema error: Element declaration for
file://home/aha/devel/cs/nshell/XmlSchemaReader:boo is missing. XML Line 7,
Position 4.
Validation Error Severity: Error
Validation Error Line Number: 7
Validation Error Line Position: 4
Validation Error Source:
Validation Error Source Schema:
Validation Error Source Uri:
Validation Error thrown from:
Validation Error callstack:
<boo>man</boo>
Validation Error Message: XmlSchema error: Invalid end element. There are
still required content items. XML Line 8, Position 3.
Validation Error Severity: Error
Validation Error Line Number: 8
Validation Error Line Position: 3
Validation Error Source:
Validation Error Source Schema:
Validation Error Source Uri:
Validation Error thrown from:
Validation Error callstack:
</note>
Why am I seeing the output of the error three times?
Likewise if I put the invalid element at the beginning I receive errors
about valid elements.
Regards
--
Aquil H. Abdullah
aquil.abdullah at gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-list/attachments/20080416/20ff1b25/attachment-0001.html
More information about the Mono-list
mailing list