Jon Skeet [C# MVP] wrote:
Can you produce a short but complete program which demonstrates the
problem?
Not exactly short, but here goes...
1. Create a new console app, and paste in this code:
using System;
using System.Xml;
using System.Xml.Sche ma;
namespace XmlTest
{
class Program
{
static void Main(string[] args)
{
// Schema and XML file names
string uriOfSchema = @"d:\test\po.xs d";
string xmlToValidate = @"d:\test\po.xm l";
// Define XmlReader settings
XmlReaderSettin gs settings = new XmlReaderSettin gs();
settings.CheckC haracters = true;
settings.CloseI nput = true;
// Try reading schema
try
{
settings.Schema s.Add(null, uriOfSchema);
}
catch (System.Xml.Xml Exception e)
{
Console.WriteLi ne("Error in " + e.SourceUri + ": " + e.Message);
return;
/* NOTE: If e.Message starts with ', everything before e.Message is
* strangely removed. */
}
settings.Valida tionType = ValidationType. Schema;
settings.Valida tionFlags |= XmlSchemaValida tionFlags.Repor tValidationWarn ings;
settings.Valida tionEventHandle r += new ValidationEvent Handler(Validat ionCallBack);
try
{
using (XmlReader xmlReader = XmlReader.Creat e(xmlToValidate , settings))
{
if (xmlReader != null)
{
while (xmlReader.Read ());
Console.WriteLi ne("Validation completed.");
}
}
}
catch (Exception e)
{
Console.WriteLi ne(e.Message);
}
}
private static void ValidationCallB ack(object sender, ValidationEvent Args args)
{
switch (args.Severity)
{
case XmlSeverityType .Error:
Console.WriteLi ne("Error: " + args.Message);
break;
case XmlSeverityType .Warning:
Console.WriteLi ne("Warning: " + args.Message);
break;
}
}
}
}
2. Then make a file called po.xsd and paste in this code:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="a" type="xs:string "/
</xs:schema>
(There is an intentional well-formedness error on line 3.)
3. Put this file in a folder of your choice and correct the variable 'uriOfSchema' in the code. You won't need the XML file to run this test.
Gustaf