Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 956
  • Last Modified:

How can I raise a custom error message in an XSD?

I have an XSD document which validates my xml files. I want to display a custom error message when one of the criteria in the XSD definition is not met by the xml file. For example, I want to validate a node which contains  a date value. If that date is not valid according to the schema definition, I want to raise custom error message saying the date is not valid.

Is it possible to raise custom error messages in xsd? If yes, how can we do that?

Looking forward to your response.

Tx
0
chaleastale
Asked:
chaleastale
  • 3
  • 2
1 Solution
 
Geert BormansCommented:
Hi chaleastale,

there are various ways to do that.

most elegant seems to me to use a parser class in any programming language you would like
(just wrap the parser)
and catch the error raised by the parser and raise an exception with a new message

you could also use Schematron to validate and choose your own messages

Cheers!
0
 
chaleastaleAuthor Commented:
I appreciate your response. Could you please send me some sample code for doing that? I haven't ever done raising a custom error message in xsd.

Thank you.
0
 
Geert BormansCommented:
well, what programming language would you use to call the parser?
0
 
chaleastaleAuthor Commented:
I am using c#.

Thanks again
0
 
Geert BormansCommented:
chaleastale,
> I am using c#.

Here is an example on how to validate XML with XSD schema in C#

http://support.microsoft.com/default.aspx?scid=kb;en-us;318504

That is what you can use to validate your XML, using C#

somewhere down there you have a catch
            catch(XmlSchemaException XmlSchExp)
            {
                Console.WriteLine(XmlSchExp.Message);
            }

this one just writes the exception message to the console
Now in your program, you can check what that message is
and write an alternative to the console...

that is all there is to it

cheers
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now