473,387 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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
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);
}
}
Nov 12 '05 #1
0 1364

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: jesper | last post by:
Hi I am currently following the tutorial from IBM (http://www-106.ibm.com/developerworks/xml/edu/x-dw-xjaxb-i.html) I have three problems at the moment. 1. It says else where that when the...
2
by: nanookfan | last post by:
Hi all, I'm having a bizarre problem converting XML files to HTML using an XSLT. The problem is only occuring in my Netscape 7.0 browser. What makes it more bizarre is that it is only...
2
by: Dale Gerdemann | last post by:
I'm having trouble with Unicode encoding in DOM. As a simple example, I read in a UTF-8 encoded xml file such as: <?xml version="1.0" encoding="UTF-8" standalone="no"?> <aText>letter 'a' with...
5
by: Alexis | last post by:
Hello, I have a set of classes I created from schema files using the xsd.exe tool. I'm using namespaces in the clases ( I had to because I have some classes with the same name but not the same...
7
by: Harolds | last post by:
The code below worked in VS 2003 & dotnet framework 1.1 but now in VS 2005 the pmID is evaluated to "" instead of what the value is set to: .... xmlItems.Document = pmXML // Add the pmID...
0
by: ZippyV | last post by:
Hello everybody, I'm trying to convert a given xsd scheme to vb.net code with the help of xsd.exe. I've had no problems during conversion but the problem is that I can't build the code in vs.net....
16
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by...
4
by: Jean-François Michaud | last post by:
Hello, I've been looking at this for a bit now and I don't see what's wrong with the code. Can anybody see a problem with this? Here is an XSLT snippet I use. <xsl:template match="graphic">...
3
by: Jon L | last post by:
Hi, I'm hoping someone can help me with this problem. I'm not sure whether the problem lies with the software or with my understanding of the language. I'm using the Microsoft.XMLDOM object...
3
by: John Carlyle-Clarke | last post by:
Hi. I'm new to Python and trying to use it to solve a specific problem. I have an XML file in which I need to locate a specific text node and replace the contents with some other text. The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.