建立一個(gè)WinForm應(yīng)用程序添加MenuStrip控件,填寫兩個(gè)功能“讀取” 和“導(dǎo)出數(shù)據(jù)”。
用了兩個(gè)DataSet控件和對話框“打開(OpenFilesDialog控件)"和"保存(SaveFilesDialog控件)"
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (opFileDlg .ShowDialog() == DialogResult.OK)
{
if(opFileDlg .OpenFile()!=null)
{
twoXML .ReadXml (@opFileDlg .FileName );
foreach (DataRow twoRow in twoXML .Tables ["user"].Rows)
{
DataRow newRow = dsXML.Tables["user"].NewRow();
newRow ["序號"] = twoRow ["序號"];
newRow["標(biāo)題"] = twoRow["標(biāo)題"];
newRow["網(wǎng)址"] = twoRow["網(wǎng)址"];
newRow["用戶名"] = twoRow["用戶名"];
newRow["密碼"] = twoRow["密碼"];
newRow["時(shí)間"] = twoRow["時(shí)間"];
newRow["備注"] = twoRow["備注"];
dsXML .Tables ["user"].Rows .Add(newRow);
}
int n = dsXML .Tables ["user"].Rows .Count ;
for(int i=0;i<n;i++)
{
dsXML .Tables ["user"].Rows [i]["序號"]=i+1;
}
dsXML.WriteXml(@"user.xml");
this.Visible = true;
MessageBox.Show("數(shù)據(jù)導(dǎo)入成功!", "成功");
}
}
else
{
this.Visible = true;
}
}
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (svFileDlg.ShowDialog() == DialogResult.OK)
{
dsXML.WriteXml(@svFileDlg.FileName);
this.Visible = true;
MessageBox.Show("數(shù)據(jù)導(dǎo)出成功!", "成功");
}
else
{
this.Visible = true;
}
}