gpt4 book ai didi

c# - 为什么 XmlDocument Validate 事件处理程序没有被命中?

转载 作者:行者123 更新时间:2023-11-30 22:28:11 26 4
gpt4 key购买 nike

我有这个代码:

  // 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 的第一行:

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" 
xmlns:xs="http://www.w3.org/2001/XMLSchema">

有什么想法吗?

最佳答案

我有一些使用这种结构验证 xml 的代码。

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

关于c# - 为什么 XmlDocument Validate 事件处理程序没有被命中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10902908/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com