I have some code running within my Web Service which validates the incoming
XML against the XSD. It is working well and picking up errors as I would
expect. However, the "message" property within
System.xml.Schema.ValidationEventArgs (which is where the error ends up)
looks a bit messy:
The 'http://header.xxxxxxxxxx.org.uk:SomeFieldName' element is invalid - The
value 'XXX' is invalid according to its datatype
'http://header.xxxxxxxxxx.org.uk:SomeDataType' - The string 'XXX' is not a
valid Integer value.
I would like to present the error in a more readable format. In other
words, I would like to get hold of "SomeFieldName", "SomeDataType", "XXX",
"Integer", etc and shovel them into some structured XML. Logically, I would
then use that structured XML within the detail element of a SOAP fault.
Is there an easy (obvious probably, but I'm missing it) way to get hold of
the elements that make up the Message? I guess I could do something by
parsing the Message text, but that doesn't feel right and is certainly not a
maintenance free approach!
Any thoughts? Thanks, Nick
John Saunders [MVP] - 14 Nov 2007 15:26 GMT
>I have some code running within my Web Service which validates the incoming
>XML against the XSD. It is working well and picking up errors as I would
[quoted text clipped - 18 lines]
>
> Any thoughts? Thanks, Nick
I think you're stuck. That Message property is just text, and I assume that
the XML Validator doesn't store the variable parts of the message anywhere
except in the message string.

Signature
--------------------------------------------------------------------------------
John Saunders | MVP - Windows Server System - Connected System Developer
Nick Locke - 14 Nov 2007 21:00 GMT
Thanks John - that was what I suspected and I certainly can't find the
variable parts anywhere. Maybe I could shovel the XML Node into an object
as the reader works its way through each element. Then I could at least
look at that node and pull together some detail......
Thanks again!
Nick
>>I have some code running within my Web Service which validates the
>>incoming XML against the XSD. It is working well and picking up errors as
[quoted text clipped - 22 lines]
> that the XML Validator doesn't store the variable parts of the message
> anywhere except in the message string.
John Saunders [MVP] - 15 Nov 2007 12:22 GMT
> Thanks John - that was what I suspected and I certainly can't find the
> variable parts anywhere. Maybe I could shovel the XML Node into an object
> as the reader works its way through each element. Then I could at least
> look at that node and pull together some detail......
Consider something like this:
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.Schemas = _schemas;
settings.ValidationFlags =
XmlSchemaValidationFlags.ReportValidationWarnings |
XmlSchemaValidationFlags.ProcessIdentityConstraints;
using (XmlReader reader = CreateXmlReader(settings))
{
settings.ValidationEventHandler +=
delegate(object sender, ValidationEventArgs e)
{
XmlReader senderAsReader = sender as XmlReader;
XmlSchemaObject sso = e.Exception.SourceSchemaObject;
// Play with senderAsReader and sso
};
XmlDocument document = new XmlDocument();
document.Load(reader);
}

Signature
--------------------------------------------------------------------------------
John Saunders | MVP - Windows Server System - Connected System Developer
Nick Locke - 19 Nov 2007 14:02 GMT
Thanks John.
>> Thanks John - that was what I suspected and I certainly can't find the
>> variable parts anywhere. Maybe I could shovel the XML Node into an
[quoted text clipped - 21 lines]
> document.Load(reader);
> }
Nick Locke - 20 Nov 2007 18:04 GMT
Voila:
' Create new Serialization(info)
Dim objInfo As New
Runtime.Serialization.SerializationInfo(e.Exception.GetType(), New
Runtime.Serialization.FormatterConverter())
e.Exception.GetObjectData(objInfo, New
Runtime.Serialization.StreamingContext(Runtime.Serialization.StreamingContextStates.All))
' Takes the values from "objInfo" and shovels them into a string array
Dim exArgs() As String = objInfo.GetValue("args",
Type.GetType("System.String[]"))
Array elements are:
0 - element name
1 - element value
2 - data type
3 - error text (also available as e.exception.innerexception.message)