问题
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