• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 196
  • Last Modified:

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
0
richjo100
Asked:
richjo100
  • 3
1 Solution
 
nonubikCommented:
Try w.Flush() before w.Close()
0
 
richjo100Author Commented:
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?
0
 
richjo100Author Commented:
Hi nonubik. I tried the w.Flush() and it didnt work
0
 
richjo100Author Commented:
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();
0
 
moduloCommented:
PAQed with points refunded (100)

modulo
Community Support Moderator
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now