c# - Deserialize the content of one of the template to check if it is in valid XML format -
i want deserialize content of 1 of template check if in valid xml format.can 1 give me sample code this?
you can use validatesample:
using system; using system.collections; using system.data; using system.io; using system.xml; using system.xml.schema; using system.text; public class xmlvalidator { // validation error count static int errorscount = 0; // validation error message static string errormessage = ""; public static void validationhandler(object sender, validationeventargs args) { errormessage = errormessage + args.message + "\r\n"; errorscount ++; } public void validate(string strxmldoc) { try { // declare local objects xmltextreader tr = null; xmlschemacollection xsc = null; xmlvalidatingreader vr = null; // text reader object tr = new xmltextreader(urlpath); xsc = new xmlschemacollection(); xsc.add(null, tr); // xml validator object vr = new xmlvalidatingreader( strxmldoc, xmlnodetype.document, null); vr.schemas.add(xsc); // add validation event handler vr.validationtype = validationtype.schema; vr.validationeventhandler += new validationeventhandler(validationhandler); // validate xml data while(vr.read()); vr.close(); // raise exception, if xml validation fails if (errorscount > 0) { throw new exception(errormessage); } // xml validation succeeded console.writeline("xml validation succeeded.\r\n"); } catch(exception error) { // xml validation failed console.writeline("xml validation failed." + "\r\n" + "error message: " + error.message); } } }
and code deserialize:
var reader = new stringreader(xmlstring); var serializer = new xmlserializer(typeof(radio)); var instance = (radio)serializer.deserialize(reader); return instance;
Comments
Post a Comment