I am interested in defining a key constraint in my Xsd. It is my understanding that using xs:key should constrain the value used to a member of a referenced list of values.<
As usual, after a good night's rest and a fresh look, spotted no fewer than 2 errors in this exercise.
XmlSchemaValidationFlags.ProcessIdentityConstraints
set by XmlReaderSettings.ValidationFlag
, and
should read
.Full working sample is as follows,
Xml sample
simple validator
[TestMethod]
public void Test_Schema()
{
string schemaFileName = @"sampleSchema.xsd";
string xmlFileName = @"sampleXml.xml";
XmlSchema schema =
XmlSchema.Read(
File.OpenText(schemaFileName),
(o, e) => { throw new Exception("BOOM"); });
XmlReaderSettings settings = new XmlReaderSettings
{
ValidationType = ValidationType.Schema,
ValidationFlags =
XmlSchemaValidationFlags.ProcessInlineSchema |
XmlSchemaValidationFlags.ProcessSchemaLocation |
XmlSchemaValidationFlags.ReportValidationWarnings |
// d'oh! explicit flag for processing identity constraints!
XmlSchemaValidationFlags.ProcessIdentityConstraints,
};
settings.Schemas.Add(schema);
settings.ValidationEventHandler +=
(o, e) => { throw new Exception("CRASH"); };
XmlReader reader = XmlReader.Create(xmlFileName, settings);
while (reader.Read()) { }
}