.net - C++\CLI - How to set a member function as a handler to xmlDocument::Schemas::ValidationEventHandler -


i'm trying set member function handler xmldocument data member in c++/cli in visual studio 2012. handler function looks this...

void validate ( system :: object ^ sender, system :: xml :: schema :: validationeventargs ^ xmlexception ) {              switch ( xmlexception->severity ) {                  case system :: xml :: schema :: xmlseveritytype :: error : {                      system :: console :: writeline ( "object: {0}", sender->tostring () );                     system :: console :: writeline ( " error: {0}", xmlexception->message );                  } break;                  case system :: xml :: schema :: xmlseveritytype :: warning : {                      system :: console :: writeline ( " object: {0}", sender->tostring () );                     system :: console :: writeline ( "warning: {0}", xmlexception->message );                  } break;                  default : {                      system :: console :: writeline ( "an unknown xml exception has occured:\n\n   object: {0}", sender->tostring () );                     system :: console :: writeline ( "exception: {0}", xmlexception->message );                  } break;              }          }; 

...which nothing special.

this data member looks like...

system :: xml :: xmldocument ^ xmldocument; 

...and how i'm trying set in class constructor:

xmldocument->schemas->validationeventhandler += gcnew system :: xml :: schema :: validationeventargs ( this, & myclass :: validate ); 

my problem receive error c3767: candidate function(s) not accessible, though "validate" member function , class defined in specified public.

i've tried number of approaches, working off of eventargs or employing "#pragma make_public ( system :: xml :: schema :: validationeventargs )", no avail.

i'm running out of hair pull out of head, appreciated. everyone.

firstly, reason getting compiler error c3767 need do:

schemas->validationeventhandler += gcnew system::xml::schema::validationeventhandler(this, &myclass::validate);  

instead of gcnew validationeventargs.

secondly, aware xmlschemaset.validationeventhandler may not think. documentation:

sets event handler receiving information schema validation errors when add or compile methods of xmlschemaset called.

thus if you're looking add event handler invalid xml document, isn't it. it's errors in schema itself.

if you're looking make class wraps xmldocument allows callers load schemas , validate xmldocument against it, this:

public ref class myclass { public:     myclass()     {         xmldocument = gcnew system::xml::xmldocument();         schemas = gcnew system::xml::schema::xmlschemaset();         // set callback errors in schema itself.         schemas->validationeventhandler += gcnew system::xml::schema::validationeventhandler(this, &myclass::validateschema);            xmldocument->schemas = schemas;     }      // adds schema current schema set.     void addschema(system::string ^targetnamespace, system::string ^schemauri);      // loads specified document, validating against current schema set.     void loaddocument(system::string ^inputuri);      // validates current document against current schema set.     void validatedocument();      system::xml::xmldocument ^ getdocument() { return xmldocument; }  private:     // validation callback errors in schema itself.     void validateschema ( system :: object ^ sender, system :: xml :: schema :: validationeventargs ^ xmlexception );      // validation callback errors in xml document.     void validatexml ( system :: object ^ sender, system :: xml :: schema :: validationeventargs ^ xmlexception );      // lower level callback both schema , xml errors.     void validate ( system :: object ^ sender, system :: xml :: schema :: validationeventargs ^ xmlexception, system::string ^ prefix );      system::xml::xmldocument ^ xmldocument;      system::xml::schema::xmlschemaset ^schemas; }; 

it convenient cache xmlschemaset separately because, when xmldocument initialized xml data xmlreader, xmldocument.schemas object (re)loaded schemas property of reader.

then prototype implementation like:

#include "stdafx.h"  #using <mscorlib.dll> #using <system.dll> #using <system.xml.dll>  using namespace system; using namespace system::xml; using namespace system::xml::schema;  void myclass::addschema(system::string ^targetnamespace, system::string ^schemauri) {     try     {         schemas->add(targetnamespace, schemauri);         xmldocument->schemas = schemas;     }     catch (exception ^ex)     {         console::writeline(ex->tostring());         // possibly rethrow.     } }  void myclass::loaddocument (system::string ^inputuri) {     xmlreader^ reader = nullptr;     try     {         validationeventhandler^ eventhandler = gcnew system::xml::schema::validationeventhandler(this, &myclass::validatexml);            xmlreadersettings^ settings = gcnew xmlreadersettings();          settings->schemas = schemas;         settings->validationtype = validationtype::schema;         settings->validationeventhandler += eventhandler;          reader = xmlreader::create(inputuri, settings);          xmldocument->load(reader);         reader->close();     }     catch (exception ^ex)     {         console::writeline(ex->message);         // possibly rethrow.     }         {         // make sure reader closed in event of exception.         delete reader;     } }  void myclass::validatedocument() {     validationeventhandler^ eventhandler = gcnew system::xml::schema::validationeventhandler(this, &myclass::validatexml);      xmldocument->validate(eventhandler); }  void myclass::validateschema ( system :: object ^ sender, system :: xml :: schema :: validationeventargs ^ xmlexception) {     validate(sender, xmlexception, gcnew system::string("schema: ")); }  void myclass::validatexml ( system :: object ^ sender, system :: xml :: schema :: validationeventargs ^ xmlexception ) {     validate(sender, xmlexception, system::string::empty); }  void myclass::validate ( system :: object ^ sender, system :: xml :: schema :: validationeventargs ^ xmlexception, system::string ^ prefix ) {     switch ( xmlexception->severity )      {     case system :: xml :: schema :: xmlseveritytype :: error :          {             system :: console :: writeline ( prefix + "object: {0}", sender->tostring () );             system :: console :: writeline ( prefix + " error: {0}", xmlexception->message );          } break;      case system :: xml :: schema :: xmlseveritytype :: warning :          {             system :: console :: writeline ( prefix + " object: {0}", sender->tostring () );             system :: console :: writeline ( prefix + "warning: {0}", xmlexception->message );          } break;      default :          {             system :: console :: writeline ( prefix + "an unknown xml exception has occured:\n\n   object: {0}", sender->tostring () );             system :: console :: writeline ( prefix + "exception: {0}", xmlexception->message );          } break;     } }; 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -