I call a function that returns a string containing XML data. How this function works is not important, but the resulting xml may be different depending on the success of the function.
Basically, the function will return either an XML request or an XML format with an error. The following are basic examples of how the two results look ...
Success:
<SpecificResult> <Something>data</Something> </SpecificResult>
On Error:
<ErrorResult> <ErrorCode>1</ErrorCode> <ErrorMessage>An Error</ErrorMessage> </ErrorResult>
As my system is configured, I can convert the xml string to a class with a simple converter function, but for this I need to know the type of class. With success, I find out that this is a SpecificResult, and I can convert. But first, I want to check if an error has occurred.
An ideal end result will allow something like this ...
string xml = GetXML(); if(!IsError(xml)) {
So the question is, what is the best way to implement the IsError
function?
I thought of several options, but not sure if I really like them ...
- check if the xml string contains
"<ErrorResult>"
- try converting xml to class ErrorResult and check for failure
- use XDocument or similar built-in functions to parse the tree and search for ErrorResult node
source share