“循环属性组引用。”

发布于 2024-10-10 19:23:21 字数 2503 浏览 3 评论 0原文

这又是一种奇怪的情况了:)

我有函数根据 XSD 验证给定的 XML,并在验证失败时抛出异常。 (该函数是从接收 xml 调用的网页调用的)

调用

if (!xmlvld.ValidXmlDoc(X, "", "https://somepathtofile.xsd")) 
    throw new Exception(xmlvld.ValidationError + "1");

和验证函数都非常简单

   /// <summary>
    /// This method validates an xml string against an xml schema.
    /// </summary>
    /// <param name="xml">StringReader containing xml</param>
    /// <param name="schemaNamespace">XML Schema Namespace</param>
    /// <param name="schemaUri">XML Schema Uri</param>
    /// <returns>bool</returns>
    public bool ValidXmlDoc(StringReader xml, string schemaNamespace, string schemaUri)
    {
        // Continue?
        if (xml == null || schemaNamespace == null || schemaUri == null)
        {
            return false;
        }

        isValidXml = true;
        XmlValidatingReader vr;
        XmlTextReader tr;
        XmlSchemaCollection schemaCol = new XmlSchemaCollection();
        schemaCol.Add(schemaNamespace, schemaUri);

        try
        {
            // Read the xml.
            tr = new XmlTextReader(xml);
            // Create the validator.
            vr = new XmlValidatingReader(tr);
            // Set the validation tyep.
            vr.ValidationType = ValidationType.Auto;
            // Add the schema.
            if (schemaCol != null)
            {
                vr.Schemas.Add(schemaCol);
            }
            // Set the validation event handler.
            vr.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
            // Read the xml schema.
            while (vr.Read())
            {
            }

            vr.Close();

            return isValidXml;
        }
        catch (Exception ex)
        {
            this.ValidationError = ex.Message;
            return false;
        }
        finally
        {
            // Clean up...
            vr = null;
            tr = null;
        }
    }

,这在过去 2 年中运行得很好,但最近验证函数开始返回以下消息:

[ValidationError]Circular attribute group reference.[/ValidationError]

这是错误消息- XSD 和发送的 XML 都没有改变,此外,如果我回收应用程序池,并再次发送相同的 XML(我有“ping”程序,每隔几秒发送相同的 XML 并检查结果),验证就会通过。 ..一段时间(有些在几分钟到几个小时之间)。

XSD 与调用页面位于同一目录中,在服务器日志中,我看到对 XSD 的请求带有 HTTP/200 响应,我什至尝试将 HTTPS 更改为本地路径,但错误仍然相同(指向无效地址)给出 HTTP/404 或无效路径异常,因此它确实尝试读取 XSD 文件)

再次 - 回收应用程序池可以立即解决它,但我不能每 5 分钟回收一次。

提前致谢! 西蒙

Its the weird-weird situation once again :)

I've function that verifies given XML against XSD and throws exception when the validation fails. (the function is called from WebPage that receives the xml calls)

both the call

if (!xmlvld.ValidXmlDoc(X, "", "https://somepathtofile.xsd")) 
    throw new Exception(xmlvld.ValidationError + "1");

and the validating function are pretty simple

   /// <summary>
    /// This method validates an xml string against an xml schema.
    /// </summary>
    /// <param name="xml">StringReader containing xml</param>
    /// <param name="schemaNamespace">XML Schema Namespace</param>
    /// <param name="schemaUri">XML Schema Uri</param>
    /// <returns>bool</returns>
    public bool ValidXmlDoc(StringReader xml, string schemaNamespace, string schemaUri)
    {
        // Continue?
        if (xml == null || schemaNamespace == null || schemaUri == null)
        {
            return false;
        }

        isValidXml = true;
        XmlValidatingReader vr;
        XmlTextReader tr;
        XmlSchemaCollection schemaCol = new XmlSchemaCollection();
        schemaCol.Add(schemaNamespace, schemaUri);

        try
        {
            // Read the xml.
            tr = new XmlTextReader(xml);
            // Create the validator.
            vr = new XmlValidatingReader(tr);
            // Set the validation tyep.
            vr.ValidationType = ValidationType.Auto;
            // Add the schema.
            if (schemaCol != null)
            {
                vr.Schemas.Add(schemaCol);
            }
            // Set the validation event handler.
            vr.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
            // Read the xml schema.
            while (vr.Read())
            {
            }

            vr.Close();

            return isValidXml;
        }
        catch (Exception ex)
        {
            this.ValidationError = ex.Message;
            return false;
        }
        finally
        {
            // Clean up...
            vr = null;
            tr = null;
        }
    }

And this worked very well for the last 2 years, but recently the validation function started to return the follwing message:

[ValidationError]Circular attribute group reference.[/ValidationError]

Which is false message - neither the XSD nor the sent XML have changed, furthermore if I recycle the Application Pool, and send the same XML again (I've "ping" program that sends the same XML every few seconds and checks the result) the validation passes... for a while (some is between few minutes and few hours).

The XSD is sitting in the same directory as the calling page and in the server log I see the request to the XSD with HTTP/200 response, I even tried changeing the HTTPS to local path but the error remained the same (pointing to invalid address gives HTTP/404 or invalid path exceptions so it does attempt to read the XSD file)

Again - recycling the apppool resolves it immediately but I can't recycle every 5 mins.

Thanks in advance!
Simon

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

给妤﹃绝世温柔 2024-10-17 19:23:21

您不会喜欢这个答案,但显然 XmlValidatingReader 在 .NET 2.0 中已过时......也许是因为这样的问题?

建议使用 进行验证XmlReader。该链接适用于 2.0 版本的帮助文件,其中包括一些特定于从 XmlValidatingReader 进行转换的详细信息;可以从顶部的下拉菜单中获取更新版本的帮助。

此链接提供有关XmlValidatingReader 已过时。

此处是使用 XmlReader 的示例。

You won't like this answer, but apparently the XmlValidatingReader was obsoleted in .NET 2.0... perhaps because of issues like this?

The suggestion is to validate using an XmlReader. That link is for the 2.0 version of the help files, which includes some details specific to converting from an XmlValidatingReader; more recent versions of the help are available from the drop-down at the top.

This link gives details about the XmlValidatingReader being obsolete.

An example of using an XmlReader is here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文