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

资源文件的配置,暂不支持编辑,如果要编辑直接删除后新增就可以了。
使用XML读写删除功能来实现资源文件配置
1.添加资源文件strings.zh-cn.resx和strings.en-us.resx
也可以参考资源文件配置和使用

页面代码:aspx

<?xml version="1.0" encoding="utf-8"?>
<root>
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="ResMimeType">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="Version">
<value>1.0.0.0</value>
</resheader>
<resheader name="Reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="Writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="LoginName">
<value>用户名</value>
</data>
<data name="Password">
<value>密码</value>
</data>
</root>

2.html代码

<HTML>
<HEAD>
<title>XMLGrid</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<script language=javascript>
function test(obj)
{
var id = obj.id;
var objs;
if(id!="")
{
var len = id.length;
var objs = obj.children;
                }
                document.getElementById("<%=TextBox1.ClientID%>").innerText=objs[1].innerText;
                document.getElementById("<%=TextBox2.ClientID%>").innerText=objs[2].innerText;
            }
</script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<TABLE id="Table1" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px" cellSpacing="1"
                cellPadding="1" width="300" border="1">
<tr>
<td colspan="2">资源文件配置</td>
</tr>
<tr>
<td>Language</td>
<td><asp:dropdownlist id="DropDownList1" runat="server" AutoPostBack="True">
<asp:ListItem Value="zh-cn">中文</asp:ListItem>
<asp:ListItem Value="en-us">英文</asp:ListItem>
</asp:dropdownlist></td>
</tr>
<TR>
<TD>Name</TD>
<TD><asp:textbox id="TextBox1" runat="server"></asp:textbox></TD>
</TR>
<TR>
<TD>Value</TD>
<TD><asp:textbox id="TextBox2" runat="server"></asp:textbox></TD>
</TR>
<TR>
<TD></TD>
<TD><asp:button id="Button1" runat="server" Text="新增"></asp:button></TD>
</TR>
</TABLE>
<asp:datagrid id="DataGrid1" style="Z-INDEX: 102; LEFT: 8px; POSITION: absolute; TOP: 160px" runat="server"
                AutoGenerateColumns="False" DataKeyField="name" BorderColor="#CC9966" BorderWidth="1px" BackColor="White"
                CellPadding="4" OnDeleteCommand="Delete" BorderStyle="None">
<FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>
<SelectedItemStyle Font-Bold="True" ForeColor="#663399" BackColor="#FFCC66"></SelectedItemStyle>
<ItemStyle ForeColor="#330099" BackColor="White"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#FFFFCC" BackColor="#990000"></HeaderStyle>
<Columns>
<asp:TemplateColumn HeaderText="序号">
<ItemTemplate>
<%# Container.ItemIndex+1 %>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="name" ReadOnly="True" HeaderText="Name"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Value">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem,"value") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID=txtvalue Runat=server Text='<%# DataBinder.Eval(Container.DataItem,"value") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:ButtonColumn Text="删除" ButtonType="PushButton" HeaderText="删除" CommandName="Delete"></asp:ButtonColumn>
</Columns>
</asp:datagrid>
</form>
</body>
</HTML>

 

3. 后台cs文件代码

public class WebForm1 : System.Web.UI.Page
{
private XmlNode node=null;
protected System.Web.UI.WebControls.DropDownList DropDownList1;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{   
                DataBind();
            }
        }

DataBind#region DataBind
private void DataBind()
{
            FileName=Server.MapPath("strings."+this.DropDownList1.SelectedValue+".resx");
            DataSet ds=new DataSet();
            ds.ReadXml(FileName);
this.DataGrid1.DataSource=ds;
this.DataGrid1.DataBind();
        }
#endregion

SetValue#region SetValue
public void SetValue(string key, string Value)
{
            XmlDocument doc = new XmlDocument();
            doc.Load(FileName);  
            node =  doc.SelectSingleNode("//root");     
if( node!= null )
{
                XmlElement addElem= (XmlElement)node.SelectSingleNode("//data[@name='" +key +"']") ;
if(addElem!=null)
{                   
                    AlertError("该元素已经存在了。");
                }
else
{
                    XmlElement first = doc.CreateElement("data");
                    first.SetAttribute("name",key);
                    node.AppendChild(first);

                    XmlElement second = doc.CreateElement("value");
                    second.InnerText=Value;
                    first.AppendChild(second);

                    saveDoc(doc,FileName);
                }
            }
        }

#endregion

saveDoc#region saveDoc
private void saveDoc(XmlDocument doc,string docPath)
{
try
{
                XmlTextWriter writer = new XmlTextWriter( docPath , null );
                writer.Formatting = Formatting.Indented;   
                doc.WriteTo( writer );   
                writer.Flush();
                writer.Close(); 
return;
            }
catch
{
throw;
            }
        }
#endregion

removeElement#region removeElement
public void removeElement(string elementKey)

            XmlDocument doc = new XmlDocument();
            doc.Load(FileName); 
            node =  doc.SelectSingleNode("//root");  
if( node != null )
{   
                node.RemoveChild( node.SelectSingleNode("//data[@name='" +elementKey +"']") );
                saveDoc(doc,FileName);
            }
        }
#endregion

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

/**//// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{   
this.DropDownList1.SelectedIndexChanged += new System.EventHandler(this.DropDownList1_SelectedIndexChanged);
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
this.DataGrid1.ItemDataBound+=new DataGridItemEventHandler(DataGrid1_ItemDataBound);
        }
#endregion

property#region property
private string FileName
{
get
{
if(ViewState["FileName"]!=null && ViewState["FileName"].ToString()!="")
{
return ViewState["FileName"].ToString();
                }
else
{
return "";
                }
            }
set
{
                ViewState["FileName"]=value;
            }
        }

#endregion

Button1_Click#region Button1_Click
private void Button1_Click(object sender, System.EventArgs e)
{
            SetValue(this.TextBox1.Text,this.TextBox2.Text);
            DataBind();
        }

#endregion

Delete#region Delete
protected void Delete(object sender,DataGridCommandEventArgs e)
{
            removeElement(this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString());
            DataBind();
        }
#endregion

AlertError#region AlertError
private void AlertError(string msg)
{
string script = "<script language='javascript'>";
            script +=" alert('" + msg +"');";
            script +="</script>";
            Page.RegisterClientScriptBlock("Alert", script);
        }
#endregion

DropDownList1_SelectedIndexChanged#region DropDownList1_SelectedIndexChanged
private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
            DataBind();
        }
#endregion

DataGrid1_ItemDataBound#region DataGrid1_ItemDataBound
private void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
                e.Item.Attributes.Add("onclick","test(this);");
                e.Item.ID = "ItemID";
            }
        }
#endregion
    }

你可能感兴趣的:(xml)