I have got an XML file, that aderes to a XDR-Schema which references
elements and attributes in two other XDR-Schemas.
I want to validate this XML file before I create a DOM tree in C#. This
works fine for every namespace except of the default namespace. In the
default namespace there is no Event when the XML file is wrong.
Could anybody tell me what there is wrong with my code (as seen below)? I
have tried several things, but I could not make the validation work
properly.
The class simply creates a DOM form a XML file and writes every error out in
the console.
Best regards,
Daniel Haag
public class XmlVerify
{
bool m_documentValid = true;
string m_path = @"C:\FDT XML Schemas\";
string m_filename;
/// <summary>
/// Validates an XML file with its schema.
/// </summary>
/// <param name="stream">Stream to the XmlFile</param>
/// <param name="schemapath">Directory with schema files</param>
/// <returns>XmlDocument</returns>
public XmlDocument ValidateXml(string filename,string schemapath)
{
m_filename = filename;
XmlValidatingReader valReader=null;
XmlDocument doc=null;
try
{
// Create ValidatingReader
valReader = new
XmlValidatingReader(stream,XmlNodeType.Document,nu ll);
// Append EventHandler
valReader.ValidationEventHandler +=new
System.Xml.Schema.ValidationEventHandler(valReader _ValidationEventHandler);
// Set Validation Type
valReader.ValidationType = ValidationType.XDR;
// Add Schemas to Collection if XDR is not Referenced in XML
XmlSchemaCollection schemaCollection =
GetSchemaCollection(schemapath);
// Add SchemaCollection to Validtaingreader's Schema property
valReader.Schemas.Add(schemaCollection);
doc= new XmlDocument(valReader.NameTable);
// Parse XmlData
doc.Load(valReader);
}
catch (XmlException e)
{
Console.WriteLine(e.Message);
}
finally
{
if (valReader!=null)
{
// Close Stream
valReader.Close();
}
}
// Return document
if (m_documentValid)
{
Console.WriteLine("Document valid:\t{0}", m_filename);
return doc;
}
else
{
Console.WriteLine("Document invalid:\t{0}", m_filename);
m_documentValid=true;
return null;
}
}
// -------------------------------------------------------------------------
----------------
/// <summary>
/// Returns a XmlSchemaCollection for all xdr schemas in the directory.
/// </summary>
/// <param name="directoy">Path to the schema files</param>
/// <returns>Schemas</returns>
public XmlSchemaCollection GetSchemaCollection(string directoy)
{
// instantiate SchemaCollection
XmlSchemaCollection schemas = new XmlSchemaCollection();
string [] fileEntries = System.IO.Directory.GetFiles(directoy, "*.xml");
foreach(string file in fileEntries)
{
// Set Validation EventHandler
schemas.ValidationEventHandler +=new
ValidationEventHandler(Schema_ValidationEventHandl er);
// Get namespace
FileInfo fileinfo = new FileInfo(file);
string namespaceString = "x-schema:"+fileinfo.Name;
// Add schema to schemacollection
schemas.Add(namespaceString,fileinfo.FullName);
}
return schemas;
}
private void valReader_ValidationEventHandler(object sender,
System.Xml.Schema.ValidationEventArgs e)
{
// 3. Write handling code
Console.WriteLine("DocumuentValidation{0}:\n{1}",e .Severity,e.Message);
m_documentValid = false;
}
private void Schema_ValidationEventHandler(object sender,
System.Xml.Schema.ValidationEventArgs e)
{
// 5.3 Write handling code
Console.WriteLine("SchemaValidation{0}:\n{1}",e.Se verity, e.Message);
}
}