I'm using the XDocument.Validate
extension method to validate my instance. Is there anyway to hold an XML instance accountable to its own schema reference? This seems like something that would be fairly implicit. Unfortunately simply loading the instance into an XDocument
doesn't seem to perform this validation implicitly.
views:
22answers:
1
+1
A:
If you want to validate on load try to use:
XDocument.Load Method (XmlReader, LoadOptions)
with validating XMLReader.
For example, something like this:
XmlReader reader;
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings |
XmlSchemaValidationFlags.ProcessSchemaLocation;
ValidationEventHandler validator = delegate(object sender,
ValidationEventArgs e)
{
Console.WriteLine("{0}: {1}", e.Severity, e.Message);
};
settings.ValidationEventHandler += validator;
settings.CloseInput = true;
StringReader sr = new StringReader(inputXml);
reader = XmlReader.Create(sr, settings);
Shcheklein
2010-09-09 18:50:50