2011-10-03 67 views
1


我写了一些代码,在C#窗体中,绑定到验证按钮。
代码在这里:my validation button。该代码正在使用XSD模式检查XML文件的验证。如果发生异常,则会将异常文本引发到文本框中,并且程序正在停止验证。 我想将错误/异常记录到数组中,然后将错误打印到文本框中。
如何做到这一点?如何在C#中记录异常的所有错误,然后显示?

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

回答

0

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

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

还有没有其他办法? – sunpietro

+0

如果要创建自己的文本,可以通过阅读Exception的Message和StackTrace属性以及可能的InnerException来创建要显示的消息文本。 –

0

应打开XmlReader与​​对象,并使用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); 

    } 
} 
+0

它开始向我发送XML无效的通知;> – sunpietro

相关问题