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

时间:2011-10-03 09:02:16

标签: c# winforms exception


我在C#窗体中编写了一些代码,绑定到验证按钮 代码在这里:my validation button。代码检查使用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);
    }
}

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

您应该使用XmlReader对象打开XmlReaderSettings并使用ValidationEventHandler来捕获错误并将其报告给用户。

请参阅XmlReaderSettings.ValidationEventHandler Event

上的完整文档和工作示例

基本上写这样的东西:

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);

  }  
}