C# WinForm 读写XML文件

时间:2022-11-20 10:00:44

建立一个WinForm应用程序 添加MenuStrip控件,填写两个功能“读取” 和“导出数据”。

用了两个DataSet控件和对话框“打开(OpenFilesDialog控件)"和"保存(SaveFilesDialog控件)"

 

读取

 

C# WinForm 读写XML文件         private   void  读取ToolStripMenuItem_Click( object  sender, EventArgs e)
C# WinForm 读写XML文件C# WinForm 读写XML文件        
{
C# WinForm 读写XML文件             
if (opFileDlg .ShowDialog() == DialogResult.OK)
C# WinForm 读写XML文件C# WinForm 读写XML文件            
{
C# WinForm 读写XML文件                 
if(opFileDlg .OpenFile()!=null)
C# WinForm 读写XML文件C# WinForm 读写XML文件                 
{
C# WinForm 读写XML文件                     twoXML .ReadXml (@opFileDlg .FileName );
C# WinForm 读写XML文件                     
foreach (DataRow twoRow in twoXML .Tables ["user"].Rows)
C# WinForm 读写XML文件C# WinForm 读写XML文件                     
{
C# WinForm 读写XML文件                         DataRow newRow 
= dsXML.Tables["user"].NewRow();
C# WinForm 读写XML文件                        newRow [
"序号"]  = twoRow ["序号"];
C# WinForm 读写XML文件                        newRow[
"标题"= twoRow["标题"];
C# WinForm 读写XML文件                        newRow[
"网址"= twoRow["网址"];
C# WinForm 读写XML文件                        newRow[
"用户名"= twoRow["用户名"];
C# WinForm 读写XML文件                        newRow[
"密码"= twoRow["密码"];
C# WinForm 读写XML文件                        newRow[
"时间"= twoRow["时间"];
C# WinForm 读写XML文件                        newRow[
"备注"= twoRow["备注"]; 
C# WinForm 读写XML文件                        dsXML .Tables [
"user"].Rows .Add(newRow);
C# WinForm 读写XML文件 
C# WinForm 读写XML文件                     }

C# WinForm 读写XML文件                     
int n = dsXML .Tables ["user"].Rows .Count ;
C# WinForm 读写XML文件                     
for(int i=0;i<n;i++)
C# WinForm 读写XML文件C# WinForm 读写XML文件                     
{
C# WinForm 读写XML文件                         dsXML .Tables [
"user"].Rows [i]["序号"]=i+1;
C# WinForm 读写XML文件                     }

C# WinForm 读写XML文件                     dsXML.WriteXml(
@"user.xml");
C# WinForm 读写XML文件                     
this.Visible = true
C# WinForm 读写XML文件                     MessageBox.Show(
"数据导入成功!""成功");
C# WinForm 读写XML文件                 }

C# WinForm 读写XML文件            }

C# WinForm 读写XML文件            
else
C# WinForm 读写XML文件C# WinForm 读写XML文件            
{
C# WinForm 读写XML文件               
this.Visible  = true;
C# WinForm 读写XML文件            }

C# WinForm 读写XML文件        }

 

导出

C# WinForm 读写XML文件         private   void  导出ToolStripMenuItem_Click( object  sender, EventArgs e)
C# WinForm 读写XML文件C# WinForm 读写XML文件        
{
C# WinForm 读写XML文件            
if (svFileDlg.ShowDialog() == DialogResult.OK)
C# WinForm 读写XML文件C# WinForm 读写XML文件            
{
C# WinForm 读写XML文件                dsXML.WriteXml(@svFileDlg.FileName);
C# WinForm 读写XML文件                
this.Visible = true;
C# WinForm 读写XML文件                MessageBox.Show(
"数据导出成功!""成功");
C# WinForm 读写XML文件            }

C# WinForm 读写XML文件            
else
C# WinForm 读写XML文件C# WinForm 读写XML文件            
{
C# WinForm 读写XML文件               
this.Visible  = true;
C# WinForm 读写XML文件            }

C# WinForm 读写XML文件        }