คำตอบ ValidationEvent only fires once

  • 7 กันยายน 2548 13:07
     
     
    Using XMLReader with a ValidationEventHandler (VC# beta 2), a schema and the flag set to report warnings. When I introduce errors to the XML file, only the first is reported, although the processing continues fine. Am I doing something wrong? I want it to validate the whole file and write warnings to a log.

    Thanks in advance.

ตอบทั้งหมด

  • 13 กันยายน 2548 6:25
     
     
    You will get multiple warnings repoorted, it just depends how you introduced the errors into the XML file. Can you post the file and the expected schema?

    Thanks, Mark Fussell
    WSE PM
  • 13 กันยายน 2548 17:56
     
     

    I've been working on this and have found that it does fire multiple times if the introduced errors are to do with the schema validation, so wrong datatype, missing element etc.. However, failures on well-formedness and the like throw an exception, which I don't want them to do. The help implies that it should fire the event and that I can test the severity level for 'error' or 'warning' if I want to. It says it only throws an exception if there's no event handler. Is the help wrong? or is there a way for me to tell it not to throw the exception? (if there is I can't find it).

    It won't let me post enough of the file and schema to make sense, so hopefully you might know the answer without?

    Thanks very much

    Lesley

  • 13 ตุลาคม 2548 6:40
     
     
    It is true that the XmlTextReader only gives you the first syntax error then it stops.  This is by design as the System.Xml runtime components are not designed for handling non-well-formed XML as this becomes too ambiguous.  XML Tools like the XML editor in Visual Studio 2005 therefore have their own error-recovery style parsers so that they can do clever things to recover from potentially ambiguous syntax errors.  For example, what do you do with the following syntax error?

    <node><node></node>

    Is this two sibling <node> objects where the first one was not closed before the second one was started, or is this nested node objects where the first one was not closed at the end?

    System.Xml wants to guarentee unambiguous XML processing so it can be safely used in mission critical applications. 
  • 14 ตุลาคม 2548 8:32
     
     
    I understand your point, but I would much prefer to be able to turn it off, so that I can write a program to tell the user all the things wrong with their file at once (as Visual Studio itself does, as you say) without having to write my own parser.

    Thanks for your help.
  • 15 ตุลาคม 2548 8:12
     
     คำตอบ
    Yes, that would be a great tool - and this is exactly what you get with the new XML editor in Visual Studio 2005 where all the errors have a red-squiggle under them.  The bad news is this kind of parser that can handle more than one syntax error is not packaged as a reusable framework API.