2012-01-09 37 views
0

問:爲什麼它在本地驗證,但未能在服務器

下面的代碼工作得非常好本地,但是當我在服務器上嘗試.the網頁掛(並重定向到登錄頁面)。

     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文件,檢查發生了什麼。它的工作好,但在本地服務器上,我等待很長一段時間,然後將其重定向到登錄page.i檢查IIS的事件查看器和我的錯誤文件夾,沒有發現任何內容。

+0

如果你提供了一個SSCCE(http://homepage1.nifty.com/algafield/sscce.html) – 2012-01-09 16:04:01

回答

3

將日誌記錄或跟蹤添加到您的代碼。這將幫助你追蹤哪些是錯誤的。顯然,你的服務器的設置和你的開發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見。

祝你好運!

+0

這將是一個更容易的幫助錯誤文件夾沒有寫權限:( – 2012-01-10 09:35:12

相關問題