如何在C#中记录异常的所有错误,然后显示

本文关键字:有错误 然后 显示 异常 记录 | 更新日期: 2023-09-27 18:10:22


我已经用C#窗体编写了一些绑定到验证按钮的代码
代码在这里:我的验证按钮。代码正在检查具有XSD架构的XML文件的验证。若发生异常,它会将异常文本放入文本框,程序将停止验证。我想把错误/异常记录到一个数组中,然后把错误打印到文本框中
怎么做?

private void validateButton_Click(object sender, System.EventArgs e)
{
    resultTextBox.Text = String.Empty;
    if (ValidateForm())
    {
        try
        {
            Cursor.Current = Cursors.WaitCursor;
            XmlSchemaSet schemaSet = new XmlSchemaSet();
            schemaSet.Add(String.Empty, XmlReader.Create(new StreamReader(xmlSchemaFileTextBox.Text)));
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.Schemas = schemaSet;
            settings.ValidationType = ValidationType.Schema;
            XmlReader reader = XmlReader.Create(new StringReader(inputXmlTextBox.Text), settings);
            while (reader.Read()) { }
            resultTextBox.Text = "The XML file is OK :)" +
                Environment.NewLine +
                DateTime.Now.ToLongDateString();
        }
        catch (XmlSchemaException schemaEx)
        {
            resultTextBox.Text = "The XML file is invalid:" +
                Environment.NewLine +
                schemaEx.LineNumber +
                ": " +
                schemaEx.Message;
        }
        catch (Exception ex)
        {
            resultTextBox.Text = ex.ToString();
        }
        finally
        {
            Cursor.Current = Cursors.Default;
        }
    }
    else
    {
        MessageBox.Show(null, "You have to load XML and XSD files to validate.", "There's XML file reading error.", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

如何在C#中记录异常的所有错误,然后显示

如果要向应用程序的用户显示异常,可以使用ExceptionMessageBox

try {
  throw new ApplicationException("test");
}
catch (ApplicationException ex)
{   
  ExceptionMessageBox box = new ExceptionMessageBox(ex);
  box.Show(this);
}

您应该使用XmlReaderSettings对象打开XmlReader,并使用ValidationEventHandler捕获错误并向用户报告。

请参阅以下位置的完整文档和工作示例:XmlReaderSettings。ValidationEventHandler事件

基本上是这样写的:

using System;
using System.Xml;
using System.Xml.Schema;
using System.IO;
public class ValidXSD {
  public static void Main() {
    // Set the validation settings.
    XmlReaderSettings settings = new XmlReaderSettings();
    settings.ValidationType = ValidationType.Schema;
    settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema;
    settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
    settings.ValidationEventHandler += new ValidationEventHandler (ValidationCallBack);
    // Create the XmlReader object.
    XmlReader reader = XmlReader.Create("inlineSchema.xml", settings);
    // Parse the file. 
    while (reader.Read());
  }
  // Display any warnings or errors.
  private static void ValidationCallBack (object sender, ValidationEventArgs args) {
     if (args.Severity==XmlSeverityType.Warning)
       Console.WriteLine("'tWarning: Matching schema not found.  No validation occurred." + args.Message);
     else
        Console.WriteLine("'tValidation error: " + args.Message);
  }  
}