简体   繁体   English

在C#中通过XSD验证XML,并且可以访问无效元素

[英]Validate XML over XSD in C# with access to invalid element

I want to validate my XML file having XSD schemas. 我想验证具有XSD架构的XML文件。 I use: 我用:

var settings = new XmlReaderSettings();
settings.ValidationFlags = XmlSchemaValidationFlags.AllowXmlAttributes
    | XmlSchemaValidationFlags.ProcessIdentityConstraints
    | XmlSchemaValidationFlags.ProcessInlineSchema        
    | XmlSchemaValidationFlags.ReportValidationWarnings;
settings.ValidationType = ValidationType.Schema;
settings.ValidationEventHandler += new ValidationEventHandler(settings_ValidationEventHandler);
settings.Schemas.Add(xsc);
var vreader = XmlReader.Create(stream, settings);
while (vreader.Read());

And if some error occured then I have beautiful human-readable information in my handler. 如果发生了一些错误,那么我的处理程序中就会有漂亮的人类可读信息。 But I want to know the node which caused the error. 但我想知道导致错误的节点。 There is no access to object like XmlElement or something like this. 无法访问像XmlElement这样的对象或类似的东西。 Is there a way to do it? 有办法吗?

I am not aware of an easy way on how to do this... I would start by looking at the SchemaInfo property associated with a node; 我不知道如何执行此操作的简单方法...我将首先查看与节点关联的SchemaInfo属性; intuitively, depending on the error, you might not get it at all... 直观地说,根据错误,你可能根本不会得到它......

The only other "key" between these might be the line/col number, which then can be cross indexed (you need to read this then, is using Xml.Linq). 这些之间唯一的另一个“关键”可能是行/列号,然后可以进行交叉索引(您需要阅读它,然后使用Xml.Linq)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM