Link to home
Start Free TrialLog in
Avatar of richjo100
richjo100

asked on

Writing xml with mfc app

Hello,

I want to write an xml file on a button click.
Under the button I have

XmlTextWriter w = new XmlTextWriter("D:\\rich\\test.xml");
w.WriteStartElement("x","root","urn:1");
w.WriteStartElement("y","item","urn:1");
w.WriteEndElement();
w.WriteEndElement();
w.Close();

but I get an error message saying that w cannot be destroyed. I got this code from the ms web stie and cannot work out why it is saying it.

I can write a blank empty file by
CreateFile(D:\\rich\\test.xml, FILE_WRITE_DATA, FILE_SHARE_READ, NULL, CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL);
but I need it to contain some xml dependent on the options selected in my MFC form

Thanks in advance

Richard
Avatar of nonubik
nonubik

Try w.Flush() before w.Close()
Avatar of richjo100

ASKER

Hi nonubik,
I'll try that over the next couple of days. I was wondering if it was to do with the scope of my declaration. Should I declare the xmltextwriter outside of my button click?
Hi nonubik. I tried the w.Flush() and it didnt work
Worked it out. I think I was getting confused with C# and c++

String* path = new String("D:/rich/test.xml");
XmlTextWriter* w =new XmlTextWriter(path, 0);                   
w->WriteStartElement("MetaData");
w->WriteAttributeString("Tracks", "V");
w->WriteAttributeString("Logged", "1");
w->WriteEndElement();
ASKER CERTIFIED SOLUTION
Avatar of modulo
modulo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial