Checking XSD with an XML reader, collecting validation errors. (WITH#)

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

+4
source share
1 answer

You may need to consider the following things:

  • In the general case, it is not always possible to β€œcollect” all errors, simply because parser checks may refuse the verification process when certain types of errors occur, especially those that put the validator in a state in which it cannot be reliably restored. For example, the validator can continue to work after it is launched in a restrictive violation of the face for a simple type, but will skip the entire section if it starts in unexpected content.

  • Unlike parsing in the DOM, where DOM loading is not affected by validation of the reader that does not validate , deserialization into an object (or at least should be) is completely different: the DOM is about to be well-formed; deserialization, i.e. strong typing, is valid .

Intuitively, I would think that if you get a validation error, what's the point of continuing deserialization and further validation?

Try validating your XML regardless of deserialization. If you really get more errors noted by this approach, then the above should explain why. If not, then you are pursuing something else.

0
source

Source: https://habr.com/ru/post/1441830/


All Articles