//write to XML void __fastcall TForm1::Button2Click(TObject *Sender) { AnsiString flist[128][6]; //將 friend list data 放至 array for(int row=1;row<=DBGrid1->DataSource->DataSet->RecordCount;row++) { DBGrid1->DataSource->DataSet->RecNo=row; for(int col=0;colDataSource->DataSet->FieldCount;col++) { flist[row-1][col]= DBGrid1->DataSource->DataSet->Fields->Fields[col]->AsString; } } //****************將 array 的內容寫入 XML**************// if (FileExists("friend_list.xml")) DeleteFile("friend_list.xml"); _di_IXMLDocument froot=NewXMLDocument(); //create XML froot->SetVersion("1.0"); froot->SetEncoding("UTF-16"); froot->SetDocumentElement(froot->CreateElement("temp","")); _di_IXMLNode sroot=froot->DocumentElement; _di_IXMLNode slist; for(int row=1;row<=DBGrid1->DataSource->DataSet->RecordCount;row++) { DBGrid1->DataSource->DataSet->RecNo=row; for(int col=0;colDataSource->DataSet->FieldCount;col++) { slist = sroot->AddChild("aa"); slist->Text = flist[row-1][col]; } } froot->SaveToFile("friend_list.xml"); //save XML delete froot; Memo1->Lines->Add("寫入XML完成");