dynamically add values to resources (C#)

依然范特西╮ 提交于 2020-01-15 10:57:08

问题


I want to add values to the resources dynamically through codings(C#). My below coding runs without any error but the values are not getting added to the resource file.

protected void Button2_Click(object sender, EventArgs e)
{
    using (ResXResourceWriter resx = new ResXResourceWriter("Resources.resx"))
    {
        resx.AddResource( "joth", "joth");
        resx.Close();
    }
}      

回答1:


protected void Button2_Click(object sender, EventArgs e)
{
    using (ResXResourceWriter resx = new ResXResourceWriter("Resources.resx"))
    {   resx.AddResource( "joth", "joth");
        resx.Save();
        resx.Close();
    }
} 



回答2:


I tried the above it doesn't seem to work, I looked around and try editing the resx file like a xml file and it worked for me.

<data name="v13" xml:space="preserve">
   <value>Test TEst</value>
</data>

Above is the structure of a single key/value pair in the resx file opened in nodepadd ++

XDocument doc = XDocument.Load(Server.MapPath(@"~\App_GlobalResources\myResource2.resx"));

XElement data = new XElement("data");

XNamespace ns = "xml";
data.Add(new XAttribute("name", "v13"));
data.Add(new XAttribute(XNamespace.Xml + "space", "preserve"));

data.Add(new XElement("value", "Test TEst"));

doc.Element("root").Add(data);
doc.Save(Server.MapPath(@"~\App_GlobalResources\myResource2.resx"));


来源:https://stackoverflow.com/questions/16003365/dynamically-add-values-to-resources-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!