Modifying App_GlobalResources

To modify the resources file programmatically using C# there are several techniques one of the technique is using XML.

Basically the XML format of a resource file will be like:

<data name=”Language” xml:space=”preserve”>
    <value>Language</value>
</data>
<data>name=”Language_Name” xml:space=”preserve”>
    <value>US-English</value>
</data>
data name=”Welcome” xml:space=”preserve”>
    <value>Welcome</value>
</data>

Taking this part of the resource file and do editing. To read a resource file we need ResXResourceReader Class. To add this System.Windows.Forms assembly of this class, we add a code part in web.config file.
<assemblies />
     <add assembly=”System.Windows.Forms,
    Version=2.0.0.0, Culture=neutral,
    PublicKeyToken=B77A5C561934E089″ />
</assemblies />

To read the resource file keys

  string filename = Request.PhysicalApplicationPath + App_GlobalResources\\resource.resx” + 

            Stream stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
            ResXResourceReader RrX = new ResXResourceReader(stream);
            IDictionaryEnumerator RrEn = RrX.GetEnumerator();
            SortedList slist = new SortedList();
            while (RrEn.MoveNext())
            {
                slist.Add(RrEn.Key, RrEn.Value);
            }
            RrX.Close();
            stream.Dispose();

Update the resource file

filename = Request.PhysicalApplicationPath + App_GlobalResources\\” + filename;
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(filename);
        XmlNodeList nlist = xmlDoc.GetElementsByTagName(“
data);
        XmlNode childnode = nlist.Item(id);
        childnode.Attributes[“
xml:space].Value = “default;              
        xmlDoc.Save(filename);
        XmlNode lastnode = childnode.SelectSingleNode(“
value);
        lastnode.InnerText = txtResourceValue.Text;
        xmlDoc.Save(filename);

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s