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">S tream to the XmlFile</param>
/// <param name="schemapat h">Directory with schema files</param>
/// <returns>XmlDoc ument</returns>
public XmlDocument ValidateXml(str ing filename,string schemapath)
{
m_filename = filename;
XmlValidatingRe ader valReader=null;
XmlDocument doc=null;
try
{
// Create ValidatingReade r
valReader = new
XmlValidatingRe ader(stream,Xml NodeType.Docume nt,null);
// Append EventHandler
valReader.Valid ationEventHandl er +=new
System.Xml.Sche ma.ValidationEv entHandler(valR eader_Validatio nEventHandler);
// Set Validation Type
valReader.Valid ationType = ValidationType. XDR;
// Add Schemas to Collection if XDR is not Referenced in XML
XmlSchemaCollec tion schemaCollectio n =
GetSchemaCollec tion(schemapath );
// Add SchemaCollectio n to Validtaingreade r's Schema property
valReader.Schem as.Add(schemaCo llection);
doc= new XmlDocument(val Reader.NameTabl e);
// Parse XmlData
doc.Load(valRea der);
}
catch (XmlException e)
{
Console.WriteLi ne(e.Message);
}
finally
{
if (valReader!=nul l)
{
// Close Stream
valReader.Close ();
}
}
// Return document
if (m_documentVali d)
{
Console.WriteLi ne("Document valid:\t{0}", m_filename);
return doc;
}
else
{
Console.WriteLi ne("Document invalid:\t{0}", m_filename);
m_documentValid =true;
return null;
}
}
// -------------------------------------------------------------------------
----------------
/// <summary>
/// Returns a XmlSchemaCollec tion for all xdr schemas in the directory.
/// </summary>
/// <param name="directoy" >Path to the schema files</param>
/// <returns>Schema s</returns>
public XmlSchemaCollec tion GetSchemaCollec tion(string directoy)
{
// instantiate SchemaCollectio n
XmlSchemaCollec tion schemas = new XmlSchemaCollec tion();
string [] fileEntries = System.IO.Direc tory.GetFiles(d irectoy, "*.xml");
foreach(string file in fileEntries)
{
// Set Validation EventHandler
schemas.Validat ionEventHandler +=new
ValidationEvent Handler(Schema_ ValidationEvent Handler);
// Get namespace
FileInfo fileinfo = new FileInfo(file);
string namespaceString = "x-schema:"+filein fo.Name;
// Add schema to schemacollectio n
schemas.Add(nam espaceString,fi leinfo.FullName );
}
return schemas;
}
private void valReader_Valid ationEventHandl er(object sender,
System.Xml.Sche ma.ValidationEv entArgs e)
{
// 3. Write handling code
Console.WriteLi ne("DocumuentVa lidation{0}:\n{ 1}",e.Severity, e.Message);
m_documentValid = false;
}
private void Schema_Validati onEventHandler( object sender,
System.Xml.Sche ma.ValidationEv entArgs e)
{
// 5.3 Write handling code
Console.WriteLi ne("SchemaValid ation{0}:\n{1}" ,e.Severity, e.Message);
}
}
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">S tream to the XmlFile</param>
/// <param name="schemapat h">Directory with schema files</param>
/// <returns>XmlDoc ument</returns>
public XmlDocument ValidateXml(str ing filename,string schemapath)
{
m_filename = filename;
XmlValidatingRe ader valReader=null;
XmlDocument doc=null;
try
{
// Create ValidatingReade r
valReader = new
XmlValidatingRe ader(stream,Xml NodeType.Docume nt,null);
// Append EventHandler
valReader.Valid ationEventHandl er +=new
System.Xml.Sche ma.ValidationEv entHandler(valR eader_Validatio nEventHandler);
// Set Validation Type
valReader.Valid ationType = ValidationType. XDR;
// Add Schemas to Collection if XDR is not Referenced in XML
XmlSchemaCollec tion schemaCollectio n =
GetSchemaCollec tion(schemapath );
// Add SchemaCollectio n to Validtaingreade r's Schema property
valReader.Schem as.Add(schemaCo llection);
doc= new XmlDocument(val Reader.NameTabl e);
// Parse XmlData
doc.Load(valRea der);
}
catch (XmlException e)
{
Console.WriteLi ne(e.Message);
}
finally
{
if (valReader!=nul l)
{
// Close Stream
valReader.Close ();
}
}
// Return document
if (m_documentVali d)
{
Console.WriteLi ne("Document valid:\t{0}", m_filename);
return doc;
}
else
{
Console.WriteLi ne("Document invalid:\t{0}", m_filename);
m_documentValid =true;
return null;
}
}
// -------------------------------------------------------------------------
----------------
/// <summary>
/// Returns a XmlSchemaCollec tion for all xdr schemas in the directory.
/// </summary>
/// <param name="directoy" >Path to the schema files</param>
/// <returns>Schema s</returns>
public XmlSchemaCollec tion GetSchemaCollec tion(string directoy)
{
// instantiate SchemaCollectio n
XmlSchemaCollec tion schemas = new XmlSchemaCollec tion();
string [] fileEntries = System.IO.Direc tory.GetFiles(d irectoy, "*.xml");
foreach(string file in fileEntries)
{
// Set Validation EventHandler
schemas.Validat ionEventHandler +=new
ValidationEvent Handler(Schema_ ValidationEvent Handler);
// Get namespace
FileInfo fileinfo = new FileInfo(file);
string namespaceString = "x-schema:"+filein fo.Name;
// Add schema to schemacollectio n
schemas.Add(nam espaceString,fi leinfo.FullName );
}
return schemas;
}
private void valReader_Valid ationEventHandl er(object sender,
System.Xml.Sche ma.ValidationEv entArgs e)
{
// 3. Write handling code
Console.WriteLi ne("DocumuentVa lidation{0}:\n{ 1}",e.Severity, e.Message);
m_documentValid = false;
}
private void Schema_Validati onEventHandler( object sender,
System.Xml.Sche ma.ValidationEv entArgs e)
{
// 5.3 Write handling code
Console.WriteLi ne("SchemaValid ation{0}:\n{1}" ,e.Severity, e.Message);
}
}