I am currently struggling with using XMLSerializer to perform XSD validation and collect validation errors in files. The task is to check the file based on user XSD files containing information about the set, information about the presence, etc.
My problem is this: when using XMLReader, it stops on the first error if we attach the listener to the ValidationEvents reader (via XMLReaderSettings). So I just catch the exception where I am reporting an error. While everything is in order, problems begin to appear after the registration of the exception. Immediately after this, the XMLReader is sent to the end of the failed field tag, but I cannot check the next field due to an unexplained exception.
To put this into practice, here is my code where I will catch the exception:
private bool TryDeserialize(XmlSerializer ser, XmlReader read,out object item) { string Itemname = read.Name; XmlReader read2 = read.ReadSubtree(); try { item= ser.Deserialize(read2); return true; } catch (Exception e) { _ErrorList.Add("XSD error at " + Itemname + ": " + e.InnerException.Message); item = null; return false; } }
This procedure works well, but the following is problematic. Suppose I pass the following XML fragment of this code:
<a>2885</a> <b>ABC</b> <c>5</c>
Suppose that βbβ may not have βABCβ as the value, so I get an XSD error. At the end of this, the xmlreader will be at 'EndElement, Name = b' from which I simply cannot move until I get an exception. If I do xmlreader.read, then I get the following exception (shorten the namespace here):
"e = {"The element 'urn:iso:.....b' cannot contain child element 'urn:iso:.....:c' because the parent element content model is text only."}"
After that, the xmlreader is in 'Element, Name = c', so it seems good, but when I try to deserialize it using the code above, I get the following exception:
'_message = "The transition from the 'ValidateElement' method to the 'ValidateText' method is not allowed."'
I really donβt see how I can overcome this. I tried without a second reader reading the subtree, but I have the same problem. Please offer me something, I'm really stuck. Thank you very much in advance!
greets