1

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 ?

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
E235
  • 11,560
  • 24
  • 91
  • 141
  • C++/Cli is a specific type of project. I use tinyxml v2 available on GitHub instead for c++. See https://github.com/leethomason/tinyxml2 – Andrew Truckle Feb 28 '18 at 15:36
  • What header declares `XMLDocument`? – DBedrenko Feb 28 '18 at 15:38
  • XMLDocument is a .Net class for use in C# etc. I guess that is why it has to be in a C++/CLI project as managed code. That is why I used a simple library. – Andrew Truckle Feb 28 '18 at 15:39
  • @Andrew Truckle I download tinyxml v2, compiled it as `.lib` file and added it's fullpath to the linker: Project Properties->Linker->Input->Additional Dependencies. I follow the `xmltext.cpp` and tried to load XML content and in one of the example they are using: `XMLDocument doc;` and when I tried it, I receive the error from my question. – E235 Feb 28 '18 at 15:40
  • Theirs is in a namespace tinyxml2::XMLDocument. So you got to use the namespace. Else you add a “using tinyxml2” to it. – Andrew Truckle Feb 28 '18 at 15:42
  • @DBedrenko It is declared in tinyxml: https://github.com/leethomason/tinyxml2/blob/master/tinyxml2.h#L1650 – E235 Feb 28 '18 at 15:42
  • @E235 so `#include tinyxml2.h` and link against that library, and reference that class with `tinyxml2::XMLDocument` – DBedrenko Feb 28 '18 at 15:43
  • 2
    @E235: Your problem might be that the name `XMLDocument` is rather generic. If you mean tinyxml2's `XMLDocument`, you must include "tinyxml2" in your web search, or you'll get irrelevant and misleading results. – MSalters Feb 28 '18 at 15:45

1 Answers1

2

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.

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
  • I added `tinyxml2.h` and `tinyxml2.cpp` to my project like you wrote. Is there something else I need to do ? it looks it accept it but now I have different compile errors. – E235 Feb 28 '18 at 15:51
  • As I have stated, you can't just add the files to the project. You must use the `tinyxml2::` prefix to your function call. You do not need any of those other included headers / namespaces you referred to in your question. Take that code out. – Andrew Truckle Feb 28 '18 at 15:53
  • OK, now it works. I also needed to add `#include "stdafx.h"` to `tinyxml2.cpp`. Thanks. – E235 Feb 28 '18 at 15:53
  • No, you do not need to do that. Please see updated answer. – Andrew Truckle Feb 28 '18 at 15:58
  • 1
    Nice ! Thank you very much for your explanations. – E235 Feb 28 '18 at 16:00