XmlValidatingReader: continue parsing after XmlException

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • George W.

    XmlValidatingReader: continue parsing after XmlException

    When attempting to validate an XML file, If the file is valid, it
    validates correctly, and it will catch most ValidationEvent s without
    problem. However, I keep getting the following Exception:
    System.Xml.XmlE xception: The 'RDaaaa' start tag on line '4' doesn't
    match the end tag of 'RD' in file 'file:///C:/Work/WIMAR
    XML/WI_M_1.xml'. Line 4, position 475.
    at System.Xml.XmlT extReader.Parse Tag()
    at System.Xml.XmlT extReader.Parse BeginTagExpandC harEntities()
    at System.Xml.XmlT extReader.Read( )
    at System.Xml.XmlV alidatingReader .ReadWithCollec tTextToken()
    at System.Xml.XmlV alidatingReader .Read()
    at XMLValidator.XM LEvaluator.Eval uate(String configPath) in
    c:\documents and settings\gwalke r\my documents\visua l studio
    projects\xmlval idator\xmlevalu ator.cs:line 43

    Well, this technically is doing what it's supposed to be doing, but I'd
    rather have XmlExceptions fired as Events rather than throwing
    exceptions. With a try-finally block, I want to easily be able to get
    back into reading the XML file after an exception like this. (I am sort
    of a newbie in all this, so apologies if this is a really really simple
    question.)

    I know that my XML and schema are valid. I introduced the error for
    testing purposes.
    I'm using the following example code from MSDN:
    public void Evaluate(string configPath)
    {
    /**
    * if file is okay, return 1
    * if file is not okay, return error string
    */
    XmlTextReader evaluated = new XmlTextReader(c onfigPath);
    XmlValidatingRe ader val = new XmlValidatingRe ader(evaluated) ;


    try
    {
    Console.WriteLi ne("Validating XML file ");

    // Set the validation event handler
    val.ValidationE ventHandler += new
    ValidationEvent Handler(this.Va lidationEventHa ndle);

    // Read XML data
    while (val.Read()){}

    Console.WriteLi ne ("Validation finished. Validation {0}",
    (m_success==tru e ? "successful " : "failed"));
    }
    finally
    {
    //Close the reader.
    if (val != null)
    val.Close();
    }
    }// end of Evaluate method

    private void ValidationEvent Handle (object sender, ValidationEvent Args
    args)
    {
    m_success = false;
    Console.WriteLi ne("Validation error: " + args.Message );
    }
  • Zafar Abbas [MSFT]

    #2
    Re: XmlValidatingRe ader: continue parsing after XmlException

    >Well, this technically is doing what it's supposed to be doing, but I'd[color=blue]
    >rather have XmlExceptions fired as Events rather than throwing
    >exceptions. With a try-finally block, I want to easily be able to get
    >back into reading the XML file after an exception like this. (I am sort
    >of a newbie in all this, so apologies if this is a really really simple
    >question.)[/color]

    The Validation event handler only catches violations of the schema in the
    XML document. But the Xml Document need to be well formed to begin with.
    Parsing can NOT continue after encountering a well formedness errors.


    Comment

    Working...