c# - Get a error list after validate xml against a schema file -
i´m validating xml file against schema xsd. far good, code generates exception in case of failure.
bool isvalid = true; list<string> errorlist = new list<string>(); try { xmlreadersettings settings = new xmlreadersettings(); settings.schemas.add(null, schemafilepath); settings.validationtype = validationtype.schema; xmldocument document = new xmldocument(); document.loadxml(xml); xmlreader rdr = xmlreader.create(new stringreader(document.innerxml), settings); while (rdr.read()) { } } catch (exception ex) { errorlist.add(ex.message); isvalid = false; } logerrors(errorlist); return isvalid;
but need code build list of errors found in validate before send log, instead of show first 1 found.
any thoughts?
you can use validate
method validationeventhandler
. can follow msdn's way of creating validationeventhandler
separately or inline if want.
e.g
//...other code above xmldocument document = new xmldocument(); document.load(pathxmlcons); document.validate((o, e) => { //do error logging through e.message });
if don't this, xmlschemavalidationexception
thrown , 1 can caught.
Comments
Post a Comment