473,394 Members | 1,875 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

XmlException and the XmlDocument class

I thought this might be useful for someone...

I was doing a little work earlier using an Xmldocument class to create &
maintain a minor store of data in Xml format.

I'd gotten the whole thing up and going and I decided to start testing
'disaster' scenarios.

For example, you start up but your file has been deleted, or you try to save
and the file is read only. Or you lack write permissions. Or you try to load
and the document is empty. Or the XML inside the document is malformed
because someone edited it manually.

I noticed that on the call to the XmlDocument's .Load(<stream>) method if
the document was empty it would throw an XmlException.

Unfortunately unlike much of the structured exception handling in .Net
that's a generic exception for a lot of different types of events. Other than
the LineNumber , LinePosition and Message properties, you're not really
getting much information there to uniquely identify specific Xml based
exceptions. In my case, I was getting an XmlException with a message of "Root
element is missing.". Yeah that tells me as the developer, but how do I
handle it other than catching XmlException. Catching generic exceptions is
bad practice.

In my case, if document.Load( ) failed that was ok. Because later in that
same method I'm actually setting up the document properly anyway before I
call .Save(). But, if I put a try/catch around .Load( ) and catch
XmlException, I would be catching everything related to XML coming out of
that call. Whether it be the root element missing or someone forgot a '>' on
line 182 column 12. I'd want to catch the former and allow the latter to go
right on by. Don't want to swallow all exceptions, after all. You can't try
to determine the type of exception based on the message, because that message
could be language dependent. What if I run it on a Cyrillic machine. Trying
to match "Root element is missing" in English against say Spanish isn't going
to match.

Looking over the XmlException object, I noticed it had a HRESULT property on
it in my watch window. That's great! I thought. I can uniquely identify this
exception and handle that case. Unfortunately the HResult property is a
protected member of the class. So, I couldn't grab hold if it directly to
even put it in a Switch to do something slightly more structured.

I also tried to use reflection to get that property's value, but that didn't
work well.

Then it occurred to me. It's a Protected method. So all I need to do is
derive a class from XmlException and that will give me access to the
property. I can then use that to identify the specific exception.

I then created a NeoXmlException class as a new base and created a
XmlRootElementMissingException that derived from it. I put a try/catch around
my call to document.Load( ), catching the generic XmlException. I then pass
that exception to an intermediary factory style class which if it wraps a
NeoXmlException around it, giving access to the protected HRESULT property.
If the factory can identify the type of specific XML exception it wraps one
of those specific classes around it and throws it. It it can't, it just
rethrows the original exception. As it stands right now I only have the
XmlRootElementMissingException class and everything else goes through like a
18-wheeler through a sedan. But as I find more errors that need this
treatment it's easy enough to add another class inheriting from XmlException
and add it to the factory.

I don't really understand why MS didn't either make separate exceptions in
the first place or give public access to HRESULT so we can do it ourselves.
Almost everything throws very specific exceptions which you can catch and
most books I've read say that catching generic exceptions ( the Exception
base class for example ) are very bad practice.

I just thought I'd post it here since it seems like a useful (if convoluted)
solution to this problem. Of course if anyone has a better idea lemme know so
I can revise my XML IO classes.
Aug 29 '08 #1
3 3512
On Aug 30, 12:21*am, Ed Kramer <EdKra...@discussions.microsoft.com>
wrote:
Looking over the XmlException object, I noticed it had a HRESULT propertyon
it in my watch window. That's great! I thought. I can uniquely identify this
exception and handle that case.
Have you actually checked whether HRESULT is unique for the specific
exception you're trying to catch?

By the way, you should also check InnerException. Quite often, that's
where the actual cause of the problem is - and you can check its type
to see what it was.
Aug 31 '08 #2
With more testing it seems that same HRESULT comes up for different problems
and not just the one I noted above. Which is basically throwing a
monkeywrench into things.

as far as inner exception, it's showing up as 'null' in the debug window as
is the property 'helplink' and the Values collection on the Data property
dictionary is empty. The only properties I really see with anything in them
are the message property, the stacktrace and the exception.Source which just
says "System.Xml" and isn't very helpful.
"Pavel Minaev" wrote:
On Aug 30, 12:21 am, Ed Kramer <EdKra...@discussions.microsoft.com>
wrote:
Looking over the XmlException object, I noticed it had a HRESULT property on
it in my watch window. That's great! I thought. I can uniquely identify this
exception and handle that case.

Have you actually checked whether HRESULT is unique for the specific
exception you're trying to catch?

By the way, you should also check InnerException. Quite often, that's
where the actual cause of the problem is - and you can check its type
to see what it was.
Sep 5 '08 #3
"Ed Kramer" <Ed******@discussions.microsoft.comwrote in message
news:72**********************************@microsof t.com...
With more testing it seems that same HRESULT comes up for different
problems
and not just the one I noted above. Which is basically throwing a
monkeywrench into things.

as far as inner exception, it's showing up as 'null' in the debug window
as
is the property 'helplink' and the Values collection on the Data property
dictionary is empty. The only properties I really see with anything in
them
are the message property, the stacktrace and the exception.Source which
just
says "System.Xml" and isn't very helpful.
Usually, exceptions thrown by XmlDocument have a pretty detailed error
message explaining the cause of the problem (with line number in the XML) in
its Message property. If XML input comes from the user, you can just display
that to him. If you want localization, you can deploy .NET Framework
language packs on the end user machines - it'll localize the exception
messages, too.

If you're just trying to do some quiet internal recovery, then perhaps it is
better to use something like XmlReader. Since it reads input XML
token-by-token, it will only throw the exception once you get to the point
in input XML that is faulty, and you can keep track of the state to know
precisely where and what it is.
Sep 6 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: KathyB | last post by:
If someone could just explain this to me...I just don't get it! I have an aspx page where I retrieve several session variables and use xmlDocument to transform xml file with xsl file into an...
3
by: Todd | last post by:
Our ASP.NET (C#) application accepts form entry and saves inputed data in XML. We are finding that users are sometimes cutting and pasting special characters (from MS Word) into these forms....
1
by: edi | last post by:
Hi, I have MS .Net Framework v1.1.4322. I have this XML file: <?xml version="1.0" ?> <!--here there are two spaces at the beginning--> <aaa> <a id="1"> <Dept>Finance</Dept>
3
by: Mungo Jerrie | last post by:
Hi there :-) I have some problems with the same instance of my xmldocument across 3 different classes. See code below: class one { protected static XmlDocument doc = new XmlDocument(); ...
0
by: Frederico Guimar?es via DotNetMonster.com | last post by:
Hi, I'm trying to use the Microsoft.Web.Services2.Messaging. ISoapFormatter but I receive this error: System.Xml.XmlException: The root element is missing. at System.Xml.XmlTextReader.Read()...
1
by: mohit | last post by:
Hi, I am making a web application in Web Matrix on .NET framework. I have a Login.aspx File and a Users.xml file.I am reading data from Users.xml file using FileStream.The Code is DataSet ds =...
3
by: RJA | last post by:
Hiyas, Using VS .net 2003. Setting up a Webservice that accepts 3rd party vendor designed XML requests and returns a filled XMLDocument with response data. Vendor XSDs were serialize into...
2
by: John Smith | last post by:
I'm writing webervice client using .Net 2.0. I have this class: public class MyWebService : SoapHttpClientProtocol { public XmlDocument validate(string url, XmlDocument xmlDocument) {...
1
by: BLUE | last post by:
In a property i do this: .... catch(XmlException e) { throw new XmlException("Error processing configuration file!",e); } Then I catch it in another class: catch(XmlException e) {
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.