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
|
public void xsdFragmentValidate(string xmlFrag)
{
XmlValidatingReader reader = null;
XmlSchemaCollection myschema = new XmlSchemaCollection();
ValidationEventHandler eventHandler = new ValidationEventHandler(XSDFragmentValidationcs.ShowCompileErrors );
MainForm mf = (MainForm)(formMf);
try
{
//Create the XmlParserContext.
XmlParserContext context = new XmlParserContext(null, null, "", XmlSpace.None);
//Implement the reader.
reader = new XmlValidatingReader(xmlFrag, XmlNodeType.Element, context);
OpenFileDialog dlgOpen = new OpenFileDialog();
if (dlgOpen.ShowDialog() == DialogResult.OK)
{
//Add the schema.
myschema.Add("http://tempuri.org/XMLSchema.xsd",dlgOpen.FileName );//dlgOpen.FileName//"c:\\SchemaGenerique.xsd"
}
//Set the schema type and add the schema to the reader.
reader.ValidationType = ValidationType.Schema;
reader.Schemas.Add(myschema);
while (reader.Read())
{}
mf.console.AppendText("Completed validating xmlfragment\n");
}
catch (XmlException XmlExp)
{
mf.console.AppendText(XmlExp.Message+"\n");
}
catch(XmlSchemaException XmlSchExp)
{
mf.console.AppendText(XmlSchExp.Message+"\n");
}
catch(Exception GenExp)
{
mf.console.AppendText(GenExp.Message+"\n");
}
}
public static void ShowCompileErrors(object sender, ValidationEventArgs args)
{
Console.WriteLine("Validation Error:"+ args.Message);
} |