代码如下:
删除按钮事件:
private void Btn_Delete_Click(object sender, System.EventArgs e)
{
string uid = Session["uid"]//取得主键;
string [] values = {uid};
OperateXmlByDataSet.DeleteXmlRows(ViewState["filename"].ToString(),"userid",values); }
OperateXmlByDataSet的DeleteXmlRow方法:{
string uid = Session["uid"]//取得主键;
string [] values = {uid};
OperateXmlByDataSet.DeleteXmlRows(ViewState["filename"].ToString(),"userid",values); }
DeleteXmlRows#region DeleteXmlRows
/**//// <summary>
/// 删除strColumn列中值为ColumnValue的行
/// </summary>
/// <param name="strXmlPath">xml相对路径</param>
/// <param name="strColumn">列名</param>
/// <param name="ColumnValue">strColumn列中值为ColumnValue的行均会被删除</param>
/// <returns></returns>
public static bool DeleteXmlRows(string strXmlPath,string strColumn,string[] ColumnValue)
{
try
{
DataSet ds = new DataSet();
ds.ReadXml(GetXmlFullPath(strXmlPath));
//先判断行数
if(ds.Tables[0].Rows.Count > 0)
{
//判断行多还是删除的值多,多的for循环放在里面
if(ColumnValue.Length > ds.Tables[0].Rows.Count)
{
for(int i=0; i < ds.Tables[0].Rows.Count; i++)
{
for(int j=0; j < ColumnValue.Length; j++)
{
if(ds.Tables[0].Rows[i][strColumn].ToString().Trim().Equals(ColumnValue[j]))
{
ds.Tables[0].Rows[i].Delete();
}
}
}
}
else
{
for(int j=0; j < ColumnValue.Length; j++)
{
for(int i=0; i < ds.Tables[0].Rows.Count; i++)
{
if(ds.Tables[0].Rows[i][strColumn].ToString().Trim().Equals(ColumnValue[j]))
{
ds.Tables[0].Rows[i].Delete();
}
}
}
}
ds.WriteXml(GetXmlFullPath(strXmlPath));
}
return true;
}
catch(Exception e)
{
string ex = e.Message;
return false;
}
}
#endregion
/**//// <summary>
/// 删除strColumn列中值为ColumnValue的行
/// </summary>
/// <param name="strXmlPath">xml相对路径</param>
/// <param name="strColumn">列名</param>
/// <param name="ColumnValue">strColumn列中值为ColumnValue的行均会被删除</param>
/// <returns></returns>
public static bool DeleteXmlRows(string strXmlPath,string strColumn,string[] ColumnValue)
{
try
{
DataSet ds = new DataSet();
ds.ReadXml(GetXmlFullPath(strXmlPath));
//先判断行数
if(ds.Tables[0].Rows.Count > 0)
{
//判断行多还是删除的值多,多的for循环放在里面
if(ColumnValue.Length > ds.Tables[0].Rows.Count)
{
for(int i=0; i < ds.Tables[0].Rows.Count; i++)
{
for(int j=0; j < ColumnValue.Length; j++)
{
if(ds.Tables[0].Rows[i][strColumn].ToString().Trim().Equals(ColumnValue[j]))
{
ds.Tables[0].Rows[i].Delete();
}
}
}
}
else
{
for(int j=0; j < ColumnValue.Length; j++)
{
for(int i=0; i < ds.Tables[0].Rows.Count; i++)
{
if(ds.Tables[0].Rows[i][strColumn].ToString().Trim().Equals(ColumnValue[j]))
{
ds.Tables[0].Rows[i].Delete();
}
}
}
}
ds.WriteXml(GetXmlFullPath(strXmlPath));
}
return true;
}
catch(Exception e)
{
string ex = e.Message;
return false;
}
}
#endregion