470,591 Members | 1,754 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,591 developers. It's quick & easy.

Problem with XML-Validation

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
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;
// 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 =
// Add SchemaCollection to Validtaingreader's Schema property
doc= new XmlDocument(valReader.NameTable);
// Parse XmlData
catch (XmlException e)
if (valReader!=null)
// Close Stream
// Return document
if (m_documentValid)
Console.WriteLine("Document valid:\t{0}", m_filename);
return doc;
Console.WriteLine("Document invalid:\t{0}", m_filename);
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
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);
Nov 12 '05 #1
0 1295

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by jesper | last post: by
2 posts views Thread by Dale Gerdemann | last post: by
5 posts views Thread by Alexis | last post: by
7 posts views Thread by Harolds | last post: by
reply views Thread by ZippyV | last post: by
4 posts views Thread by Jean-François Michaud | last post: by
3 posts views Thread by John Carlyle-Clarke | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.