Catching Parsing Exceptions

only for RuBoard

Throughout this chapter, we have shown little error handling for brevity. In your production applications, you might want to include error handling for a variety of situations. For example, if you're working with files, you might want to trap errors indicating the file was not found.

There might also be a case where the input document could not be parsed. Some instances might be where XML data was dynamically generated and did not generate well- formed XML data.

For example, the following XML document, badxml.xml , is not well formed because it has multiple root elements:

 <?xml version="1.0" encoding="utf-8" ?>  <multiple>  </multiple>  <multiple>  </multiple> 

The following code would trap the parse error after successfully loading the file:

 <%@ Page language="vb" Debug="true"%>  <%@ Import Namespace="System.Xml"%>  <script language="vb" runat="server">      Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles graphics/ccc.gif MyBase.Load          Dim doc As XmlDocument = New XmlDocument()          Try              doc.Load(Server.MapPath("badxml.xml"))          Catch parseError As System.Xml.XmlException              Response.write(parseError.ToString())          End Try      End Sub  </script> 

Remember that stream-based resources, such as XmlReader and XmlWriter , support a Close method so that you can ensure that resources are appropriately closed. Always include a test in your error handlers to ensure that you properly closed all resources.

only for RuBoard


XML and ASP. NET
XML and ASP.NET
ISBN: B000H2MXOM
EAN: N/A
Year: 2005
Pages: 184

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net