【发布时间】:2011-10-22 10:38:21
【问题描述】:
这类似于这个问题C# Get schema information when validating xml
但是,我正在使用 XDocument 来实现 LINQ。
我正在读取/解析一组 CSV 文件并转换为 XML,然后根据 XSD 架构验证 XML。
我想捕获与元素值相关的特定错误,生成更用户友好的消息,并将它们返回给用户,以便更正输入数据。我想在输出数据中包含的项目之一是一些架构信息(例如数字类型的可接受值的范围)。
在我目前的方法(我愿意改变)中,我能够捕获除架构信息之外的所有我需要的东西。
我尝试访问验证事件处理程序的ValidationEventArgs 参数中的SourceSchemaObject,但始终为空。我还尝试了 XElement 的 GetSchemaInfo ,它似乎也为空。
我正在使用 RegEx 来识别我想要捕获的特定验证错误,并通过验证事件处理程序的 sender 参数从 XElement 中获取数据。我曾考虑将架构转换为 XDocument 并通过 LINQ 获取我需要的内容,但在我看来应该有更好的选择
这是我当前的验证方法:
private List<String> this.validationWarnings;
private XDocument xDoc;
private XmlSchemaSet schemas = new XmlSchemaSet();
public List<String> Validate()
{
this.validationWarnings = new List<String>();
// the schema is read elsewhere and added to the schema set
this.xDoc.Validate(this.schemas, new ValidationEventHandler(ValidationCallBack), true);
return validationWarnings
}
这是我的回调方法:
private void ValidationCallBack(object sender, ValidationEventArgs args)
{
var element = sender as XElement;
if (element != null)
{
// this is a just a placeholder method where I will be able to extract the
// schema information and put together a user friendly message for specific
// validation errors
var message = FieldValidationMessage(element, args);
// if message is null, then the error is not one one I wish to capture for
// the user and is related to an invalid XML structure (such as missing
// elements or incorrect order). Therefore throw an exception
if (message == null)
throw new InvalidXmlFileStructureException(args.Message, args.Exception);
else
validationWarnings.Add(message);
}
}
我的回调方法中的var message = FieldValidationMessage(element, args); 行只是一个占位符,还不存在这个方法的目的是做3件事:
在
args.Message上使用 RegEx 识别特定的验证错误(这已经有效,我已经测试了我计划使用的模式)从 XDocument 中获取与导致错误的特定 XElement 相关的属性值(例如原始 CSV 中的行号和列号)
获取可用的架构信息,以便将字段类型和限制添加到输出消息中。
【问题讨论】:
标签: c# xml c#-4.0 xsd linq-to-xml