All,
I am having a tough time figuring out how to code a simple app to read an
xml file and an xsd file and validate the xml file using the xsd without
using a namespace for the schema. Help? Also, is there a .NET XML FAQ?
Thanks,
Will
Here are the details:
----------the error
Validation Error: The 'HeadCount' element is not declared. An error
occurred at file:///d:/xml/config.xml, (2, 2).
Validation Error: The 'Name' element is not declared. An error occurred
at file:///d:/xml/config.xml, (3, 4).
Validation Error: The 'Name' element is not declared. An error occurred
at file:///d:/xml/config.xml, (4, 4).
----------the error
----------config.xml
<?xml version="1.0" encoding="UTF-8"?>
<HeadCount>
<Name>Waldo Pepper</Name>
<Name>Red Pepper</Name>
</HeadCount>
----------config.xml
----------config.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name='HeadCount' type="HEADCOUNT"/>
<xsd:complexType name="HEADCOUNT">
<xsd:sequence>
<xsd:element name='Name' type='xsd:string' maxOccurs='unbounded'/>
</xsd:sequence>
<xsd:attribute name='division' type='xsd:int' use='optional'
default='8'/>
</xsd:complexType>
</xsd:schema>
----------config.xsd
----------doit.cs
class Tester
{
static void Main()
{
String strXml;
String strXsd;
strXml = @"d:\xml\config.xml";
strXsd = @"d:\xml\config.xsd";
Tester cs = new Tester();
cs.Validate(strXml, strXsd);
}
private void Validate(String strXml, String strXsd)
{
try
{
XmlSchemaCollection sc = new XmlSchemaCollection();
sc.ValidationEventHandler += new ValidationEventHandler
(ValidationCallBack);
sc.Add(null, strXsd);
XmlTextReader textReader = new XmlTextReader(strXml);
XmlValidatingReader validatingReader = new XmlValidatingReader
(textReader);
validatingReader.ValidationType = ValidationType.Schema;
XmlSchema schema = new XmlSchema();
schema.SourceUri = strXsd;
validatingReader.Schemas.Add(schema);
validatingReader.ValidationEventHandler += new
ValidationEventHandler(ValidationCallBack);
while (validatingReader.Read());
}
catch (System.Exception exc)
{
System.Console.Write("big bad error: \n" + exc.ToString());
}
}
private void ValidationCallBack(object sender, ValidationEventArgs e)
{
Console.WriteLine("Validation Error: {0}", e.Message);
}
}
----------doit.cs