I’ve found myself writing code like this quite a bit lately:
The intent is to find a specified child element, throwing an error if the element is not found.
After writing the same error checking code one or two times too many, I wrote some extension methods to make the task
simpler. With the extension method RequiredElement
, the above code simplifies quite a lot:
Here’s the definition for RequiredElement
- as you can see, the only significant difference is in the type of
exception thrown.
The related extension methods, RequiredAtribute
and RequiredElements
(plural) - included in the attachment - are
entirely similar.
XmlFormatException
is a pretty vanilla custom exception type, except for the detail of taking an Xelement
as one of
the parameters to its constructors - this is promptly case to IXmlLineInfo
allowing the exception to properly report
the location within the Xml file where the problem was detected, useful for other developers and for end users trying to
solve a problem.
Comments
blog comments powered by Disqus