MATLAB: How to prevent deployed .NET Assembly throwing Microsoft C++ exception: xsd_binder​::Malforme​dDocumentE​rror in C# VS driver project

cmalformeddocumenterrorMATLAB Compiler SDKnetstudiovisual

How to prevent deployed .NET Assembly throws Microsoft C++ exception: xsd_binder::MalformedDocumentError in C# Visual Studio driver project when enabling native code debugging?
When enabled native code debugging under VS -> Project -> _<Application Name> _Properites -> Debug in Visual Studio, the C# driver application that initializes a deployed .NET Assembly from MATLAB throws infinitely the errors ensembles the following and the program won't proceed:
Exception thrown at 0x000007FEFCEAA06D in AxonLite.exe: Microsoft C++ exception: xsd_binder::MalformedDocumentError at memory location 0x00000000003425E0.

Best Answer

With the "Enable native code debugging" project option checked, go to VS->Tools->Options, then Debugging->General and ticking the box for "Use Managed Compatibility Mode".T
The driver program should execute and perform debugging without further issue.
This issue seems to be a compatibility issue with Visual Studio debugging engine, with an excerpted explanation from Microsoft Documentation <https://docs.microsoft.com/en-us/visualstudio/debugger/general-debugging-options-dialog-box here>:
Use Managed Compatibility Mode
Replaces the default debugging engine with a legacy version to enable these scenarios:
- You are using a .NET Framework language other than C#, VB, or F# that provides its own Expression Evaluator (this includes C++/CLI).
- You want to enable Edit and Continue for C++ projects while mixed mode debugging.
Note that choosing Managed Compatibility mode disables some features that are implemented only in the default debugging engine.