I'm trying to validate an Xml fragment using an Xml Schema with the XDocument.Validate extension method. Whenever an invalid Xml fragment is used the ValidationEventHandler fires properly, however both the LineNumber and LinePosition properties of the XmlSchemaValidationException are 0.
我正在尝试使用Xml Schema和XDocument.Validate扩展方法验证Xml片段。每当使用无效的Xml片段时,ValidationEventHandler都会正确触发,但XmlSchemaValidationException的LineNumber和LinePosition属性都为0。
private bool Validate(XDocument doc)
{
bool isValid = true;
List<string> validationErrors = new List<string>();
XmlSchemaSet schemas = new XmlSchemaSet();
schemas.Add(null, "MyCustomSchema.xsd");
doc.Validate(schemas, (sender, args) =>
{
validationErrors.Add(String.Format("{0}: {1} [Ln {2} Col {3}]",
args.Severity,
args.Exception.Message,
args.Exception.LineNumber,
args.Exception.LinePosition));
isValid = false;
}, false);
return isValid;
}
My goal in the above example is to use validationErrors for informing a user as to why the validation failed. When this method is used, however, the LineNumber and LinePosition are both 0.
我在上面的示例中的目标是使用validationErrors通知用户验证失败的原因。但是,使用此方法时,LineNumber和LinePosition都为0。
The snippet seems simple enough and appears to work as expected in terms of validating against both valid and invalid Xml fragments.
该代码段似乎很简单,并且在验证有效和无效的Xml片段方面似乎按预期工作。
Thanks in advance!
提前致谢!
1 个解决方案
#1
4
You are not validating the textual representation of the Xml anymore but the object model. As a result there is no lines and positions because there is no file but XElement, XAttribute etc. objects in memory. Another helpful hint would be to ask yourself - what line and position should be returned if you modified (e.g. an elelment was added) the XDocument after it was loaded but before running validation? If you are not creating or modifying the Xml the fastest way would be to use XmlReader to validate your Xml document. As a bonus - if the input is a file or a stream - you should get line and position information in case of validation errors.
您不再验证Xml的文本表示形式,而是验证对象模型。因此,没有行和位置,因为内存中没有文件,但XElement,XAttribute等对象。另一个有用的提示是问自己 - 如果你在加载XDocument之后但在运行验证之前修改了(比如添加了一个元素),应该返回什么行和位置?如果您没有创建或修改Xml,最快的方法是使用XmlReader来验证您的Xml文档。作为奖励 - 如果输入是文件或流 - 您应该在验证错误的情况下获得行和位置信息。
#1
4
You are not validating the textual representation of the Xml anymore but the object model. As a result there is no lines and positions because there is no file but XElement, XAttribute etc. objects in memory. Another helpful hint would be to ask yourself - what line and position should be returned if you modified (e.g. an elelment was added) the XDocument after it was loaded but before running validation? If you are not creating or modifying the Xml the fastest way would be to use XmlReader to validate your Xml document. As a bonus - if the input is a file or a stream - you should get line and position information in case of validation errors.
您不再验证Xml的文本表示形式,而是验证对象模型。因此,没有行和位置,因为内存中没有文件,但XElement,XAttribute等对象。另一个有用的提示是问自己 - 如果你在加载XDocument之后但在运行验证之前修改了(比如添加了一个元素),应该返回什么行和位置?如果您没有创建或修改Xml,最快的方法是使用XmlReader来验证您的Xml文档。作为奖励 - 如果输入是文件或流 - 您应该在验证错误的情况下获得行和位置信息。