locked
Handle XML File Errors RRS feed

  • Question

  • XmlDocument xdXml = new XmlDocument();
                xdXml.Load(file);
    With the code above how can I add error checking on the file? For example if I go into the XML file that I need to load and then mismatch a node and try to load it the application will crash. Instead I would like to catch that error and report to the user that the XML file has errors in it.
    Wednesday, February 15, 2012 1:04 PM

Answers

  • Thanks for the info. Here are the solutions that I created.

    For the copy error I created this method.

            private string GetText(RichTextBox richTextBox)
            {
                // use a TextRange to fish out the Text from the Document
                TextRange textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
                return textRange.Text;
            }

    And for the file I did this 

                    try
                    {
                        xdXml.Load(file);
                    }
                    catch (Exception excep)
                    {
                        MessageBox.Show(excep.Message);
                    }

    That way if it fails, it will tell the user what part of the file failed and they can correct it.

    • Marked as answer by JeffTincher Wednesday, February 15, 2012 4:25 PM
    Wednesday, February 15, 2012 4:25 PM

All replies

  • I tried this

                XmlDocument xdXml = new XmlDocument();
                {
                    try
                    {
                        xdXml.Load(file);
                    }
                    catch (System.Exception excep)
                    {
                        MessageBox.Show(excep.Message);
                    }

    But every time that I force it to catch the error my app crashes with "OpenClipboard Failed (Exception from HRESULT:0x800401D0 (CLIPBRD_E_CANT_OPEN)"

    What did I do wrong?

    Wednesday, February 15, 2012 1:18 PM
  • I think my problem is that I need a better way to move text from one box to another.

    Clipboard.Clear();
                        winTextBox.SelectAll();
                        winTextBox.Copy();
                        buildAuditTextBox.Focus();
                        buildAuditTextBox.Paste();
                        Clipboard.Clear()

    That's the code I was using.

    Wednesday, February 15, 2012 1:53 PM
  • Regardig your first question about XML, the simple answer is that XML documents HAVE to be valid. One error and the whole document fails.

    You could use XmlDocument.Validate to check before you parse the document : http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.validate.aspx

    If you know the schema and a document fails validation, then you could try to repair/rebuild it from the schema, line by line.

    Catching the error as you have done should work. Your clipboard/window/copy issue is another matter, for which a cut/pastable complete example of the issue would help us to solve.

    Regards,
    Pete


    If you find my post helpful, please remember to "Mark As Answer" and/or "Vote as Helpful"

    Wednesday, February 15, 2012 2:08 PM
  • Thanks for the info. Here are the solutions that I created.

    For the copy error I created this method.

            private string GetText(RichTextBox richTextBox)
            {
                // use a TextRange to fish out the Text from the Document
                TextRange textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
                return textRange.Text;
            }

    And for the file I did this 

                    try
                    {
                        xdXml.Load(file);
                    }
                    catch (Exception excep)
                    {
                        MessageBox.Show(excep.Message);
                    }

    That way if it fails, it will tell the user what part of the file failed and they can correct it.

    • Marked as answer by JeffTincher Wednesday, February 15, 2012 4:25 PM
    Wednesday, February 15, 2012 4:25 PM