Search code examples
c++tinyxml2

Can't use XMLDocument - "incomplete type is not allowed"


I am trying to parse XML content.
I want to use XMLDocument but when I use it like that:

XMLDocument doc; 

I receive an error:

incomplete type is not allowed

When I searched for this issue I found that some places write examples with these libraries:

#using <mscorlib.dll>
#using <System.dll>
#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Schema;

But when I tried this I am received an error:

#using requires C++/CLI mode

What I need to do in order to be able to use XMLDocument object ?


Solution

  • If you look in the example source file xmltest.cpp you will see at the top:

    using namespace tinyxml2;
    

    So when you see code like:

    int example_1()
    {
        XMLDocument doc;
        doc.LoadFile( "resources/dream.xml" );
    
        return doc.ErrorID();
    }
    

    It is actually:

    int example_1()
    {
        tinyxml2::XMLDocument doc;
        doc.LoadFile( "resources/dream.xml" );
    
        return doc.ErrorID();
    }
    

    You must use the tinyxml2 namespace to identify the correct XMLDocument to use.


    • When you add the source files to the project, right-click the cpp file and choose properties:

    Properties

    • Next, you need to tell it not to use precompiled headers:

    Not use precompiled headers

    Now you do not need the #include stdafx.h call.


    As you can see, XMLDocument is also a Microsoft .NET Framework class:

    .NET Framework XMLDocument

    Without the tinyxml2 namespace it will default to this .NET Framework class. That requires a compatible application which, for C++, would mean a C++/CLI project.