Jothieshwar RS
Jothieshwar RS

Reputation: 25

dynamically add values to resources (C#)

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();
    }
}      

Upvotes: 1

Views: 2895

Answers (2)

Saad A
Saad A

Reputation: 1147

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"));

Upvotes: 0

Gupta Vini
Gupta Vini

Reputation: 530

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

Upvotes: 1

Related Questions