c++ add element xml file

  • Thread starter Thread starter raouia
  • Start date Start date
R

raouia

Guest
Hi

I'm writing a program to create an xml file through a ghraphic interface

I got to create my file and the elements but in the case of the modification to add a new element in a file already existing I found a problem

there is my code to creat the file

any help please? think you

public: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
if (SavePath == "")
{
XmlWriter ^writer;
OPENFILENAME ofn;
char szFileName[100] = "";
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.lpstrFilter = "xml Files (*.xml)\0*.xml\0All Files (*.*)\0*.*\0";
ofn.lpstrFile = szFileName;
ofn.nMaxFile = 100;
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
ofn.lpstrDefExt = "xml";
XmlWriterSettings ^settings = gcnew XmlWriterSettings();
String^ path = "";
if (GetSaveFileName(&ofn))
{
XmlWriterSettings ^settings = gcnew XmlWriterSettings();
settings->Indent = true;
settings->IndentChars = (" ");
settings->NewLineOnAttributes = true;
path = gcnew String(szFileName);


}

writer = XmlWriter::Create(path, settings);
SavePath = path;

try
{
writer->WriteStartDocument();
writer->WriteStartElement(textBox2->Text);
writer->WriteStartElement(balise);
writer->WriteString(textBox1->Text);
writer->WriteEndElement();
writer->WriteEndDocument();
writer->Flush();
writer->Close();
}
catch (Exception ^e)
{
Console::WriteLine("XML Writer Aborted -- {0}", e->Message);
}
finally
{
if (writer->WriteState != WriteState::Closed)
{
writer->Close();
}
}

}
else if (SavePath!="") // add new element
{
int a = 0;
XmlTextReader^ reader = gcnew XmlTextReader(SavePath);
while (reader->Read())
{
// here i must to write the code to add new element
}

}

Continue reading...
 
Back
Top