1

i'm trying to write config of my program in exe looking like that:

<test>
    <a1>
        <b>100</b>
        <c>2</c>
        <d>0</d>
    </a1>
    <a2>
        <b>100</b>
        <c>2</c>
        <d>0</d>
    </a2>
</test>
<test2>
<!-- ...-->
</test2>

I'm trying to do that with this code:

        XmlDocument^ doc = gcnew XmlDocument;
        doc->LoadXml( "<a1></a1");
            XmlElement^ newElem = doc->CreateElement( "value" );
            newElem->InnerText = "105";
            doc->DocumentElement->AppendChild( newElem );
            newElem = doc->CreateElement( "hotkey" );
            newElem->InnerText = "2";
            doc->DocumentElement->AppendChild( newElem );
            newElem = doc->CreateElement( "enable" );
            newElem->InnerText = "0";
            doc->DocumentElement->AppendChild( newElem );
        doc->LoadXml( "<a2></a2>");
            newElem = doc->CreateElement( "value" );
            newElem->InnerText = "105";
            doc->DocumentElement->AppendChild( newElem );
            newElem = doc->CreateElement( "hotkey" );
            newElem->InnerText = "2";
            doc->DocumentElement->AppendChild( newElem );
            newElem = doc->CreateElement( "enable" );
            newElem->InnerText = "0";
            doc->DocumentElement->AppendChild( newElem );

but second loadXml overvrite first, and i don't know how to do more categorys. Can somebody help me with them?

Luke
  • 2,350
  • 6
  • 26
  • 41
  • I'm sorry but I can't figure out the relationship between your first sample output and your code. – lc. Jul 10 '12 at 19:30

2 Answers2

0

It looks like you are forgetting to Save your modified XmlDocument?

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
0

Have you looked at the XmlTextWriter class instead? It's designed specifically for writing XML rather than just representing it. There are some simple examples at the bottom of the linked MSDN article that are very similar to what you want to do.

Also consider XML Serialization if you want a quick "it just works" solution.

Stephen Hewlett
  • 2,415
  • 1
  • 18
  • 31
  • i saw it but i don't know how can i create XmlTextWriter in memory, and then save it in dialog box ;/ – Luke Jul 10 '12 at 20:30
  • The `XmlTextWriter` object writes to a stream, which can be a [memory stream](http://msdn.microsoft.com/en-us/library/system.io.memorystream.aspx). You could create that and then write it to a file afterwards - see [this question](http://stackoverflow.com/questions/8624071/save-and-load-memorystream-to-from-a-file). It will work as long as the XML file is fairly short. – Stephen Hewlett Jul 11 '12 at 02:36