下面的代碼工作得非常好本地,但是當我在服務器上嘗試.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
的事件查看器和我的錯誤文件夾,沒有發現任何內容。
如果你提供了一個SSCCE(http://homepage1.nifty.com/algafield/sscce.html) – 2012-01-09 16:04:01