如何在 C# 中的 XmlReaderSettings.ValidationEventHandler 中获取无效的 XMLNode
我正在尝试使用回调验证事件为不成功的 XML 验证构建自定义错误消息。我注意到元素的发送者对象是 XMLReader,并且我从 ((XmlReader)sender).Name 获取了元素或当前节点名称,并从 ValidationEventargs.Exception.Message 获取了异常消息。我尝试通过获取当前节点的父节点来构建验证失败的当前节点的路径,
下面给出的是代码片段
XmlReaderSettings xrs = new XmlReaderSettings();
xrs.ValidationEventHandler += new ValidationEventHandler(ValidationEvent);
public void ValidationEvent(object sender, ValidationEventArgs e)
{
XmlReader xe = (XmlReader)sender;
ValidationError ve = new ValidationError();
ErrorElement = xe.Name;
ErrorMessage = e.Exception.Message;
ErrorPath = ""\\want to build the Node path
}
I am trying to construct a custom Error Message for unsuccessful XML validation using the callback validation event. I noticed that the sender object of the element is XMLReader and i got the Element or current Node name from ((XmlReader)sender).Name and exeception message from ValidationEventargs.Exception.Message. I trying to build the path of the current node failing in the validation by getting the parent nodes of the current node
Given below is the code snippet
XmlReaderSettings xrs = new XmlReaderSettings();
xrs.ValidationEventHandler += new ValidationEventHandler(ValidationEvent);
public void ValidationEvent(object sender, ValidationEventArgs e)
{
XmlReader xe = (XmlReader)sender;
ValidationError ve = new ValidationError();
ErrorElement = xe.Name;
ErrorMessage = e.Exception.Message;
ErrorPath = ""\\want to build the Node path
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据此线程,您需要使用XmlDocument.Validate代替。这是我的代码:
As per this thread, You need to use XmlDocument.Validate instead. Here's my code: