【问题标题】:why it validates locally but fails on the server为什么它在本地验证但在服务器上失败
【发布时间】:2012-02-06 03:48:19
【问题描述】:

问:

以下代码在本地运行良好,但是当我在服务器上尝试时,页面挂起(并重定向到登录页面)。

                        XDocument.Load(targetFileName);
                        XmlReaderSettings settings = new XmlReaderSettings();
                        settings.CloseInput = true;
                        settings.ValidationEventHandler += Handler;

                        settings.ValidationType = ValidationType.Schema;
                        settings.Schemas.Add(null, System.Web.HttpContext.Current.Server.MapPath("~/importSchema/IntialSchema.xsd"));
                        settings.ValidationFlags =
                             XmlSchemaValidationFlags.ReportValidationWarnings |
                        XmlSchemaValidationFlags.ProcessIdentityConstraints |
                        XmlSchemaValidationFlags.ProcessInlineSchema |
                        XmlSchemaValidationFlags.ProcessSchemaLocation;
                        using (StreamReader str_reader = new StreamReader(targetFileName))
                        {
                            using (XmlReader validatingReader = XmlReader.Create(str_reader, settings))
                            {
                                try
                                {
                                    while (validatingReader.Read())
                                    {

                                    }
                                }
                                catch (XmlValidationFailedException ex)
                                {
                                    Common.ErrMappingForInformix.WriteLog(ex.Message);
                                    this.ShowStatus("error","", 1);
                                    validationFailed = true;
                                }

                            }
                        }
                        if (validationFailed)
                        {
                            return;
                        }

 private static void Handler(object sender, ValidationEventArgs e)
        {
            if (e.Severity == XmlSeverityType.Error || e.Severity == XmlSeverityType.Warning)
            {
                string message = String.Format("Line: {0}, Position: {1} \"{2}\"",
                             e.Exception.LineNumber, e.Exception.LinePosition, e.Exception.Message);

                throw new XmlValidationFailedException(message, e.Exception);
            }
        }

[Serializable()]
    public class XmlValidationFailedException : System.Exception
    {
        public XmlValidationFailedException() : base() { }
        public XmlValidationFailedException(string message) : base(message) { }
        public XmlValidationFailedException(string message, Exception innerException) : base(message, innerException) { }

        protected XmlValidationFailedException(System.Runtime.Serialization.SerializationInfo info,
            System.Runtime.Serialization.StreamingContext context) { }
    }

我尝试无效的xml 文件来检查发生了什么。它在本地工作正常,但在服务器上我等待很长时间然后它重定向到登录页面。我检查IIS 的事件查看器和我的错误文件夹,什么也没找到。

【问题讨论】:

标签: c# asp.net exception iis xml-validation


【解决方案1】:

为您的代码添加日志记录或跟踪。这将帮助您找出问题所在。显然,服务器设置和开发 PC 之间存在某种差异,这通常是文件路径、权限或 IIS 的不同配置。通过使用跟踪,您可以将文件路径、变量值等输出到跟踪文件。

例如:

Trace.Write("Import schema directory: " + Server.MapPath("~/importSchema"));

settings.Schemas.Add(null, System.Web.HttpContext.Current.Server.MapPath("~/importSchema/IntialSchema.xsd"));

另见http://msdn.microsoft.com/en-us/library/bb386420.aspx

祝你好运!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-02
    • 2019-06-08
    • 2021-01-24
    • 2011-06-28
    • 1970-01-01
    相关资源
    最近更新 更多