开发者

Validating an XDocument instance against its own schema reference

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.


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜