本文介绍了为什么XmlDocument的验证事件处理程序没有得到打?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个code:
// Load the document
XmlDocument xmlDocument = new XmlDocument();
// use the stream and have it close when it is finished
using ( argInputStream )
{
xmlDocument.Load( argInputStream );
xmlDocument.Schemas.Add( XmlSchema.Read( argSchemaStream, null ) );
xmlDocument.Validate( ValidationEventHandler );
}
// this is not getting hit
void ValidationEventHandler( object sender, ValidationEventArgs e )
{
switch ( e.Severity )
{
case XmlSeverityType.Error:
Console.WriteLine( "Error: {0}", e.Message );
break;
case XmlSeverityType.Warning:
Console.WriteLine( "Warning {0}", e.Message );
break;
}
}
我的XSD的第一行:
top line of my XSD:
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
任何想法?
推荐答案
我有一些code。使用这种结构验证XML。
I've got some code validating xml using this construction.
var schemaReader = new XmlTextReader(argSchemaStream);
var schema = new XmlSchemaSet();
schema.Add(null, schemaReader);
var settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.Schemas.Add(schema);
settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
settings.ValidationEventHandler += new ValidationEventHandler(ValidationEventHandler);
var doc = new XmlDocument();
doc.Load(XmlReader.Create(argInputStream, settings));
这篇关于为什么XmlDocument的验证事件处理程序没有得到打?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!