1、XML数据源的文件名为Tables.xml,结构如下:
< Tables >
< SOP >
< PitchY > 0.20 </ PitchY >
< T > 0.30 </ T >
< B > 0.40 </ B >
< WM > 0.40 </ WM >
< WM1 >1 </ WM1 >
< WP > 0.50 </ WP >
< WP1 > 0 </ WP1 >
< R > 0.06 </ R >
< LM > L-1 </ LM >
< WCM > 0.22 </ WCM >
< WCP > 0.37 </ WCP >
</ SOP >
< SOP >
< PitchY > 3.00 </ PitchY >
< T > 0.99 </ T >
< B > 0.87 </ B >
< WM > 0.33 </ WM >
< WM1 > 1 </ WM1 >
< WP > 1.00 </ WP >
< WP1 > 1 </ WP1 >
< R > 0.05 </ R >
< LM > L </ LM >
< WCM >0.77 </ WCM >
< WCP > 0.80 </ WCP >
</ SOP >
< DCHIP >
< BodySize >3000 </ BodySize >
< L > 0.50 </ L >
< W > 0.60 </ W >
< D > 0.40 </ D >
< T > 0.20 </ T >
< S > 0.05 </ S >
< R > 0.05 </ R >
< Wm > 0.53 </ Wm >
< Lm > 0.45 </ Lm >
</ DCHIP >
< DCHIP >
< BodySize > 1608 </ BodySize >
< L > 0.70 </ L >
< W > 0.90 </ W >
< D > 0.80 </ D >
< T > 0.30 </ T >
< S > 0.05 </ S >
< R > 0.05 </ R >
< Wm > 0.85 </ Wm >
< Lm > 0.60 </ Lm >
</ DCHIP >
< DCHIP >
< BodySize > 6785 </ BodySize >
< L > 1.50 </ L >
< W > 5.30 </ W >
< D >5.30 </ D >
< T >0.99 </ T >
< S > 0.15 </ S >
< R > 0.05 </ R >
< Wm >9 .20 </ Wm >
< Lm > 1.30 </ Lm >
</ DCHIP >
</ Tables >
2、具体代码
string xmlPath = " Calculation/Tables.xml " ;
// 恢复GridView正常状态
private void ResetParameter()
{
gvParameter.EditIndex = - 1 ;
LoadParameter(ddlShapeType.SelectedValue);
}
// 加载XML并显示在GridView里
private void LoadParameter( string shapeType)
{
DataSet dsRule = new DataSet();
dsRule.ReadXml(xmlPath);
DataTable dtRule = dsRule.Tables[shapeType];
gvParameter.DataSource = dtRule;
gvParameter.DataBind();
}
// 新增事件
protected void lnbAddNew_Click( object sender, EventArgs e)
{
if (ddlShapeType.SelectedValue.Length == 0 )
{
lblMsg.Visible = true ;
lblMsg.Text = " Warning: Please select shape type ! " ;
return ;
}
else if (gvParameter.Rows.Count == 0 )
{
lblMsg.Visible = true ;
lblMsg.Text = " Warning: [ " +
ddlShapeType.SelectedValue + " ] table not exists ! " ;
return ;
}
DataSet ds = new DataSet();
ds.ReadXml(xmlPath);
DataTable dt = ds.Tables[ddlShapeType.SelectedValue];
DataRow dr = dt.NewRow();
dt.Rows.Add(dr);
ds.WriteXml(xmlPath); // 将修改写入Table.xml
ResetParameter();
}
// 编辑事件
protected void gvParameter_RowEditing( object sender,
GridViewEditEventArgs e)
{
gvParameter.EditIndex = e.NewEditIndex;
LoadParameter(ddlShapeType.SelectedValue);
GridViewRow row = gvParameter.Rows[e.NewEditIndex];
// 前面2列是Delete、Edit,故从第三列开始
for ( int i = 2 ; i < row.Cells.Count; i ++ )
{
TextBox tb = (TextBox)row.Cells[i].Controls[ 0 ];
tb.Width = 50 ;
}
}
// 更新事件
protected void gvParameter_RowUpdating( object sender,
GridViewUpdateEventArgs e)
{
GridViewRow row = gvParameter.Rows[e.RowIndex]; // 获得当前行
int numCell = row.Cells.Count; // 共几列单元格(包含Edit和Delete 2列)
int currentRow = row.DataItemIndex; // 对应DataSet对应的行索引
DataSet ds = new DataSet();
ds.ReadXml(xmlPath);
DataRow dr;
// 找到对应与DataSet行
dr =
ds.Tables[ddlShapeType.SelectedValue].Rows[row.DataItemIndex];
string [] str = null ; // 此数组定义表的列名
switch (ddlShapeType.SelectedValue)
{
case " SOP " :
{
str = new string []{ " PitchY " , " T " , " B " , " WM " ,
" WM1 " , " WP " , " WP1 " , " R " , " LM " , " WCM " , " WCP " };
break ;
}
case " DCHIP " :
{
str = new string [] { " BodySize " , " L " , " W " ,
" D " , " T " , " S " , " R " , " Wm " , " Lm " };
break ;
}
}
int j = 0 ;
// 从第3列开始,前面有2列是Edit和Delete
for ( int i = 2 ; i < numCell; i ++ )
{
string cText = ((TextBox)row.Cells[i].Controls[ 0 ]).Text;
dr[str[j]] = cText;
j ++ ;
}
ds.WriteXml(xmlPath); // 将修改写入Table.xml
ResetParameter();
}
// 取消修改
protected void gvParameter_RowCancelingEdit( object sender,
GridViewCancelEditEventArgs e)
{
ResetParameter();
}
// 删除事件
protected void gvParameter_RowDeleting( object sender,
GridViewDeleteEventArgs e)
{
GridViewRow row = gvParameter.Rows[e.RowIndex];
int curr = row.RowIndex;
DataSet ds = new DataSet();
ds.ReadXml(xmlPath);
DataRow dr = ds.Tables[ddlShapeType.SelectedValue].Rows[curr];
dr.Delete();
ds.WriteXml(xmlPath);
ResetParameter();
}