1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
/*
* XML Validation based on XML Schema (XSD)
*
* *** Skeletton ***
*
* by Alain VIZZINI
* ESSI - Sophia Antipolis
* Microsoft France
*/
using System;
using System.IO;
using System.Xml;
using System.Xml.Schema;
/// <summary>
/// Summary description for XMLValidator
/// </summary>
class XMLValidator
{
/// <summary>
/// True if some errors occured during the validation.
/// </summary>
private bool errors = false;
public bool Errors{
get {
return errors;
}
}
/// <summary>
/// True if some warnings occured during the validation.
/// </summary>
private bool warnings = false;
public bool Warnings{
get{
return warnings;
}
}
/// <summary>
/// The XML Schema (.xsd) file used to validate.
/// </summary>
private String xsdfile;
public String XSDFile{
get{
return xsdfile;
}
}
/// <summary>
/// The XML file to validate.
/// </summary>
private String xmlfile;
public String XMLFile{
get
{
return xmlfile;
}
}
/// <summary>
/// Builds a new instance of MyXMLValidator
/// </summary>
/// <param name="XSDFile">XML Schema (.xsd) file to use to validate</param>
/// <param name="XMLFile">XML file to validate</param>
public XMLValidator (String XSDFile, String XMLFile){
xmlfile = XMLFile;
xsdfile = XSDFile;
}
/// <summary>
/// Checks if the file is valid (concordant with the XSD file)
/// </summary>
/// <remarks>Sets the Warnings and Errors attributes</remarks>
public void Validate(){
//create a collection of XML Schema
XmlSchemaCollection xsdColl = new XmlSchemaCollection();
//add the XSD file to the collection
xsdColl.Add("", new XmlTextReader(XSDFile));
//the validating reader
XmlValidatingReader vr = new XmlValidatingReader(new XmlTextReader (XMLFile));
//we'll use XSD
vr.ValidationType = ValidationType.Schema;
//the XSD files are here
vr.Schemas.Add(xsdColl);
//what to do on error and warnings
vr.ValidationEventHandler += new ValidationEventHandler (XSDValidationHandler);
//read the file through
while(vr.Read()) { }
}
/// <summary>
/// Handler called on every ValidationEvent. Used to set Errors and Warnings flags.
/// </summary>
/// <param name="sender">Sender of the Event</param>
/// <param name="args">arguments</param>
private void XSDValidationHandler(object sender, ValidationEventArgs args)
{
if (args.Severity == XmlSeverityType.Warning)
warnings = true;
if (args.Severity == XmlSeverityType.Error)
errors = true;
}
/*********************************************************************************
*
* Under this point, static test purpose methods.
*
*********************************************************************************/
/// <summary>
/// The main entry point for the application.
/// </summary>
///
public static void Main(string[] args){
//
// TODO: Add code to start application here
//
if (args.Length < 2)
{
Console.WriteLine ( "usage:\n"+
"\tAlXMLValidator schema.xsd file.xml");
}
else
{
String XSDFile = args[0];
String XMLFile = args[1];
//TODO :
//Ajoutez ici le code pour instancier un XMLValidator, valider le document passé en paramètre,
//et afficher un resultat pour savoir si il y a eu des erreurs ou des warnings.
XMLValidator("championnatsImport.xsd", "championnatsImport.xml);
Validate();
}
}
} |
Partager