title: TinyXML2/C++ - Insert an element
body: <p>I wanted to serialize objects with XML, so I got TinyXML. However I went with the newer TinyXML2. Problem is, I can't find a tutorial anywhere, so I just read the documentation. However, I seem to be stuck with adding an element to the document.</p>
  
  <p>Could someone tell me what is wrong with my code?</p>
  
  <p>Here's my <code>demo.xml</code> file contents:</p>
  
  <pre><code>&lt;?xml version="1.0" ?&gt;
  &lt;Hello&gt;World&lt;/Hello&gt;
  </code></pre>
  
  <p>here's my <code>main()</code> method:</p>
  
  <pre><code>#include "tinyxml2/tinyxml2.h"
  using namespace tinyxml2;
  
  int main (int argc, char * const argv[]) 
  {
     XMLDocument doc;
     if (doc.LoadFile("demo.xml") == XML_SUCCESS)
     {
        XMLNode *node = doc.NewElement("foo");
        doc.InsertEndChild(node);
        doc.SaveFile("demo2.xml");
     }
  }
  </code></pre>
  
  <p>and finally, here's the <code>demo2.xml</code> file:</p>
  
  <pre><code>&lt;?xml version="1.0" ?&gt;
  &lt;Hello&gt;World&lt;/Hello&gt;
  
  &lt;foo/&gt;
  </code></pre>
  
  <p>Foo should look like this: <code>&lt;foo&gt;&lt;/foo&gt;</code></p>
  
  <p>But it doesn't for some reason. Can anyone explain why?</p>