如何在 C# 中的 XmlReaderSettings.ValidationEventHandler 中获取无效的 XMLNode

发布于 2024-12-27 05:11:22 字数 801 浏览 2 评论 0原文

我正在尝试使用回调验证事件为不成功的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

灯下孤影 2025-01-03 05:11:22

根据此线程,您需要使用XmlDocument.Validate代替。这是我的代码:

private static void ValidationErrorHandler(object sender, ValidationEventArgs e)
{
    if (e.Severity == XmlSeverityType.Error || e.Severity == XmlSeverityType.Warning)
    {      
        string offendingElementName = BuildQualifiedElementName((e.Exception as XmlSchemaValidationException));
        // meaningful validation reporting code goes here
        Console.Out.WriteLine("{0} is invalid", offendingElementName);   
     }
}

private static string BuildQualifiedElementName(XmlSchemaValidationException exception)
{
    List<string> path = new List<string>();
    XmlNode currNode = exception.SourceObject as XmlNode;
    path.Add(currNode.Name);
    while (currNode.ParentNode != null)
    {
        currNode = currNode.ParentNode;
        if (currNode.ParentNode != null)
        {
            path.Add(currNode.Name);
        }
    }
    path.Reverse();
    return string.Join(".", path);
}

As per this thread, You need to use XmlDocument.Validate instead. Here's my code:

private static void ValidationErrorHandler(object sender, ValidationEventArgs e)
{
    if (e.Severity == XmlSeverityType.Error || e.Severity == XmlSeverityType.Warning)
    {      
        string offendingElementName = BuildQualifiedElementName((e.Exception as XmlSchemaValidationException));
        // meaningful validation reporting code goes here
        Console.Out.WriteLine("{0} is invalid", offendingElementName);   
     }
}

private static string BuildQualifiedElementName(XmlSchemaValidationException exception)
{
    List<string> path = new List<string>();
    XmlNode currNode = exception.SourceObject as XmlNode;
    path.Add(currNode.Name);
    while (currNode.ParentNode != null)
    {
        currNode = currNode.ParentNode;
        if (currNode.ParentNode != null)
        {
            path.Add(currNode.Name);
        }
    }
    path.Reverse();
    return string.Join(".", path);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文