使用XML读写删除功能来实现资源文件配置

资源文件的配置,暂不支持编辑,如果要编辑直接删除后新增就可以了。
resourcegrid.JPG
1.添加资源文件strings.zh-cn.resx和strings.en-us.resx
也可以参考 资源文件配置和使用
None.gif <? xml version="1.0" encoding="utf-8" ?>
None.gif
< root >
None.gif  
< xsd:schema  id ="root"  xmlns =""  xmlns:xsd ="http://www.w3.org/2001/XMLSchema"  xmlns:msdata ="urn:schemas-microsoft-com:xml-msdata" >
None.gif    
< xsd:element  name ="root"  msdata:IsDataSet ="true" >
None.gif      
< xsd:complexType >
None.gif        
< xsd:choice  maxOccurs ="unbounded" >
None.gif          
< xsd:element  name ="data" >
None.gif            
< xsd:complexType >
None.gif              
< xsd:sequence >
None.gif                
< xsd:element  name ="value"  type ="xsd:string"  minOccurs ="0"  msdata:Ordinal ="1"   />
None.gif                
< xsd:element  name ="comment"  type ="xsd:string"  minOccurs ="0"  msdata:Ordinal ="2"   />
None.gif              
</ xsd:sequence >
None.gif              
< xsd:attribute  name ="name"  type ="xsd:string"   />
None.gif              
< xsd:attribute  name ="type"  type ="xsd:string"   />
None.gif              
< xsd:attribute  name ="mimetype"  type ="xsd:string"   />
None.gif            
</ xsd:complexType >
None.gif          
</ xsd:element >
None.gif          
< xsd:element  name ="resheader" >
None.gif            
< xsd:complexType >
None.gif              
< xsd:sequence >
None.gif                
< xsd:element  name ="value"  type ="xsd:string"  minOccurs ="0"  msdata:Ordinal ="1"   />
None.gif              
</ xsd:sequence >
None.gif              
< xsd:attribute  name ="name"  type ="xsd:string"  use ="required"   />
None.gif            
</ xsd:complexType >
None.gif          
</ xsd:element >
None.gif        
</ xsd:choice >
None.gif      
</ xsd:complexType >
None.gif    
</ xsd:element >
None.gif  
</ xsd:schema >
None.gif  
< resheader  name ="ResMimeType" >
None.gif    
< value > text/microsoft-resx </ value >
None.gif  
</ resheader >
None.gif  
< resheader  name ="Version" >
None.gif    
< value > 1.0.0.0 </ value >
None.gif  
</ resheader >
None.gif  
< resheader  name ="Reader" >
None.gif    
< value > System.Resources.ResXResourceReader, System.Windows.Forms, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 </ value >
None.gif  
</ resheader >
None.gif  
< resheader  name ="Writer" >
None.gif    
< value > System.Resources.ResXResourceWriter, System.Windows.Forms, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 </ value >
None.gif  
</ resheader >
None.gif  
< data  name ="LoginName" >
None.gif    
< value > 用户名 </ value >
None.gif  
</ data >
None.gif  
< data  name ="Password" >
None.gif    
< value > 密码 </ value >
None.gif  
</ data >   
None.gif
</ root >
2.html代码
None.gif < HTML >
None.gif    
< HEAD >
None.gif        
< title > XMLGrid </ title >
None.gif        
< meta  content ="Microsoft Visual Studio .NET 7.1"  name ="GENERATOR" >
None.gif        
< meta  content ="C#"  name ="CODE_LANGUAGE" >
None.gif        
< meta  content ="JavaScript"  name ="vs_defaultClientScript" >
None.gif        
< meta  content ="http://schemas.microsoft.com/intellisense/ie5"  name ="vs_targetSchema" >
ExpandedBlockStart.gifContractedBlock.gif        
< script  language =javascript > dot.gif
InBlock.gif            
function test(obj)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
var id = obj.id;
InBlock.gif                
var objs;
InBlock.gif                
if(id!="")
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
var len = id.length;
InBlock.gif                    
var objs = obj.children;
ExpandedSubBlockEnd.gif                }
                
InBlock.gif                document.getElementById(
"<%=TextBox1.ClientID%>").innerText=objs[1].innerText;
InBlock.gif                document.getElementById(
"<%=TextBox2.ClientID%>").innerText=objs[2].innerText;
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        
</ script >
None.gif    
</ HEAD >
None.gif    
< body  MS_POSITIONING ="GridLayout" >
None.gif        
< form  id ="Form1"  method ="post"  runat ="server" >
None.gif            
< TABLE  id ="Table1"  style ="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px"  cellSpacing ="1"
None.gif                cellPadding
="1"  width ="300"  border ="1" >
None.gif                
< tr >
None.gif                    
< td  colspan ="2" > 资源文件配置 </ td >
None.gif                
</ tr >
None.gif                
< tr >
None.gif                    
< td > Language </ td >
None.gif                    
< td >< asp:dropdownlist  id ="DropDownList1"  runat ="server"  AutoPostBack ="True" >
None.gif                            
< asp:ListItem  Value ="zh-cn" > 中文 </ asp:ListItem >
None.gif                            
< asp:ListItem  Value ="en-us" > 英文 </ asp:ListItem >
None.gif                        
</ asp:dropdownlist ></ td >
None.gif                
</ tr >
None.gif                
< TR >
None.gif                    
< TD > Name </ TD >
None.gif                    
< TD >< asp:textbox  id ="TextBox1"  runat ="server" ></ asp:textbox ></ TD >
None.gif                
</ TR >
None.gif                
< TR >
None.gif                    
< TD > Value </ TD >
None.gif                    
< TD >< asp:textbox  id ="TextBox2"  runat ="server" ></ asp:textbox ></ TD >
None.gif                
</ TR >
None.gif                
< TR >
None.gif                    
< TD ></ TD >
None.gif                    
< TD >< asp:button  id ="Button1"  runat ="server"  Text ="新增" ></ asp:button ></ TD >
None.gif                
</ TR >
None.gif            
</ TABLE >
None.gif            
< asp:datagrid  id ="DataGrid1"  style ="Z-INDEX: 102; LEFT: 8px; POSITION: absolute; TOP: 160px"  runat ="server"
None.gif                AutoGenerateColumns
="False"  DataKeyField ="name"  BorderColor ="#CC9966"  BorderWidth ="1px"  BackColor ="White"
None.gif                CellPadding
="4"  OnDeleteCommand ="Delete"  BorderStyle ="None" >
None.gif                
< FooterStyle  ForeColor ="#330099"  BackColor ="#FFFFCC" ></ FooterStyle >
None.gif                
< SelectedItemStyle  Font-Bold ="True"  ForeColor ="#663399"  BackColor ="#FFCC66" ></ SelectedItemStyle >
None.gif                
< ItemStyle  ForeColor ="#330099"  BackColor ="White" ></ ItemStyle >
None.gif                
< HeaderStyle  Font-Bold ="True"  ForeColor ="#FFFFCC"  BackColor ="#990000" ></ HeaderStyle >
None.gif                
< Columns >
None.gif                    
< asp:TemplateColumn  HeaderText ="序号" >
None.gif                        
< ItemTemplate >
ExpandedBlockStart.gifContractedBlock.gif                            
<% dot.gif # Container.ItemIndex+1  %>
None.gif                        
</ ItemTemplate >
None.gif                    
</ asp:TemplateColumn >
None.gif                    
< asp:BoundColumn  DataField ="name"  ReadOnly ="True"  HeaderText ="Name" ></ asp:BoundColumn >
None.gif                    
< asp:TemplateColumn  HeaderText ="Value" >
None.gif                        
< ItemTemplate >
ExpandedBlockStart.gifContractedBlock.gif                            
<% dot.gif # DataBinder.Eval(Container.DataItem,"value" %>
None.gif                        
</ ItemTemplate >
None.gif                        
< EditItemTemplate >
None.gif                            
< asp:TextBox  ID =txtvalue  Runat =server  Text ='<%#  DataBinder.Eval(Container.DataItem,"value") % > '>
None.gif                            
</ asp:TextBox >
None.gif                        
</ EditItemTemplate >
None.gif                    
</ asp:TemplateColumn >
None.gif                    
< asp:ButtonColumn  Text ="删除"  ButtonType ="PushButton"  HeaderText ="删除"  CommandName ="Delete" ></ asp:ButtonColumn >
None.gif                
</ Columns >
None.gif            
</ asp:datagrid >
None.gif        
</ form >
None.gif    
</ body >
None.gif
</ HTML >
3. cs代码
None.gif public   class  WebForm1 : System.Web.UI.Page
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
private XmlNode node=null;
InBlock.gif        
protected System.Web.UI.WebControls.DropDownList DropDownList1;
InBlock.gif        
protected System.Web.UI.WebControls.TextBox TextBox1;
InBlock.gif        
protected System.Web.UI.WebControls.TextBox TextBox2;
InBlock.gif        
protected System.Web.UI.WebControls.Button Button1;
InBlock.gif        
protected System.Web.UI.WebControls.DataGrid DataGrid1;
InBlock.gif        
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(!Page.IsPostBack)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{    
InBlock.gif                DataBind();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
DataBind#region DataBind
InBlock.gif        
private void DataBind()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            FileName
=Server.MapPath("strings."+this.DropDownList1.SelectedValue+".resx");
InBlock.gif            DataSet ds
=new DataSet();
InBlock.gif            ds.ReadXml(FileName);
InBlock.gif            
this.DataGrid1.DataSource=ds;
InBlock.gif            
this.DataGrid1.DataBind();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
SetValue#region SetValue
InBlock.gif        
public void SetValue(string key, string Value)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            XmlDocument doc 
= new XmlDocument();
InBlock.gif            doc.Load(FileName);   
InBlock.gif            node 
=  doc.SelectSingleNode("//root");      
InBlock.gif            
if( node!= null )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                XmlElement addElem
= (XmlElement)node.SelectSingleNode("//data[@name='" +key +"']") ;
InBlock.gif                
if(addElem!=null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{                    
InBlock.gif                    AlertError(
"该元素已经存在了。");
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    XmlElement first 
= doc.CreateElement("data");
InBlock.gif                    first.SetAttribute(
"name",key);
InBlock.gif                    node.AppendChild(first);
InBlock.gif
InBlock.gif                    XmlElement second 
= doc.CreateElement("value");
InBlock.gif                    second.InnerText
=Value;
InBlock.gif                    first.AppendChild(second);
InBlock.gif                   
InBlock.gif                    saveDoc(doc,FileName);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }
 
ExpandedSubBlockEnd.gif        }
  
InBlock.gif        
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
saveDoc#region saveDoc
InBlock.gif        
private void saveDoc(XmlDocument doc,string docPath)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                XmlTextWriter writer 
= new XmlTextWriter( docPath , null ); 
InBlock.gif                writer.Formatting 
= Formatting.Indented;    
InBlock.gif                doc.WriteTo( writer );    
InBlock.gif                writer.Flush(); 
InBlock.gif                writer.Close();  
InBlock.gif                
return;
ExpandedSubBlockEnd.gif            }
  
InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
throw;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }
         
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
removeElement#region removeElement
InBlock.gif        
public void removeElement(string elementKey) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{  
InBlock.gif            XmlDocument doc 
= new XmlDocument(); 
InBlock.gif            doc.Load(FileName);  
InBlock.gif            node 
=  doc.SelectSingleNode("//root");   
InBlock.gif            
if( node != null )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{    
InBlock.gif                node.RemoveChild( node.SelectSingleNode(
"//data[@name='" +elementKey +"']") ); 
InBlock.gif                saveDoc(doc,FileName);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Web Form Designer generated code#region Web Form Designer generated code
InBlock.gif        
override protected void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif            
base.OnInit(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Required method for Designer support - do not modify
InBlock.gif        
/// the contents of this method with the code editor.
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{    
InBlock.gif            
this.DropDownList1.SelectedIndexChanged += new System.EventHandler(this.DropDownList1_SelectedIndexChanged);
InBlock.gif            
this.Button1.Click += new System.EventHandler(this.Button1_Click);
InBlock.gif            
this.Load += new System.EventHandler(this.Page_Load);
InBlock.gif            
this.DataGrid1.ItemDataBound+=new DataGridItemEventHandler(DataGrid1_ItemDataBound);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
property#region property
InBlock.gif        
private string FileName
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(ViewState["FileName"]!=null && ViewState["FileName"].ToString()!="")
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return ViewState["FileName"].ToString();
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return "";
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ViewState[
"FileName"]=value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Button1_Click#region Button1_Click
InBlock.gif        
private void Button1_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SetValue(
this.TextBox1.Text,this.TextBox2.Text);
InBlock.gif            DataBind();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Delete#region Delete 
InBlock.gif        
protected void Delete(object sender,DataGridCommandEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            removeElement(
this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString());
InBlock.gif            DataBind();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
AlertError#region AlertError
InBlock.gif        
private void AlertError(string msg)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string script = "<script language='javascript'>";
InBlock.gif            script 
+=" alert('" + msg +"');";
InBlock.gif            script 
+="</script>";
InBlock.gif            Page.RegisterClientScriptBlock(
"Alert", script);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
DropDownList1_SelectedIndexChanged#region DropDownList1_SelectedIndexChanged
InBlock.gif        
private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DataBind();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
DataGrid1_ItemDataBound#region DataGrid1_ItemDataBound
InBlock.gif        
private void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                e.Item.Attributes.Add(
"onclick","test(this);");
InBlock.gif                e.Item.ID 
= "ItemID";
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

ExpandedBlockEnd.gif    }

转载于:https://www.cnblogs.com/cheatlove/articles/405380.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值