Click here to Skip to main content
15,885,546 members
Articles / Web Development / ASP.NET
Article

Editing a Resource File Treating It as an XML File

Rate me:
Please Sign up or sign in to vote.
3.44/5 (7 votes)
11 Apr 2007CPOL1 min read 95.8K   1.8K   25   15
This demo shows how to edit a resource file content by treating it as an XML File

Introduction

This is my third article. It is a continuation of my previous article. I have used the same example here.

Here, we are editing a resource file content by treating it as an XML File.

The Code

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

XML
<data name="Language" xml:space="preserve">
    <value>Language</value>
</data>
<data>name="Language_Name" xml:space="preserve">

Take this part of the resource file and do editing. First the contents of the resource file are read and displayed in a Grid. To read a resource file, we need the ResXResourceReader class. To add the System.Windows.Forms assembly of this class, we add a code part in the web.config file.

XML
<assemblies>
     <add assembly="System.Windows.Forms, 
    Version=2.0.0.0, Culture=neutral, 
    PublicKeyToken=B77A5C561934E089" />
</assemblies>

First the resource files that are available in the App_GlobalResource folder are added to a DropDownlist. We need to add the following assemblies too:

C#
using System.Globalization;
using System.Resources;
using System.IO;

Then, in the page load event:

C#
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string resourcespath = 
                Request.PhysicalApplicationPath + "App_GlobalResources";
            DirectoryInfo dirInfo = new DirectoryInfo(resourcespath);
            foreach (FileInfo filInfo in dirInfo.GetFiles())
            {
                string filename = filInfo.Name;
                cmbResources.Items.Add(filename);
            }
            cmbResources.Items.Insert(0, new ListItem("Select a Resource File"));
        }
    }

The output will be:

Screenshot - output1.jpg

Then, in the Dropdownlist SelectedIndexChanged event:

C#
protected void cmbResources_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (cmbResources.SelectedIndex != 0)
        {
            string filename = Request.PhysicalApplicationPath + 
                "App_GlobalResources\\" + cmbResources.SelectedItem.Text;
            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();
            gridView1.DataSource = slist;
            gridView1.DataBind();
        }
    }

The output will be:

Screenshot - output2.jpg

Then by clicking the edit link, it redirects to a new page where the key, value are taken in a textbox.

In the page load event, we have the code shown below. Here the ResXResourceSet class is used to get the resource values.

C#
protected void Page_Load(object sender, EventArgs e)
   {
       if (!IsPostBack)
       {
           filename = Request.QueryString["file"];
           filename = Request.PhysicalApplicationPath +
               "App_GlobalResources\\" + filename;
           string key = Request.QueryString["key"];
           Label1.Text = key;
           ResXResourceSet rset = new ResXResourceSet(filename);
           txtResourceValue.Text = rset.GetString(key);
       }
   }

Screenshot - output3.jpg

By clicking the update button, the following event triggers:

C#
protected void Button1_Click(object sender, EventArgs e)
    {
        filename = Request.QueryString["file"];
        int id = Convert.ToInt32(Request.QueryString["id"]);
        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);
        Label2.Text = "Resource File Updated...";
    }

Points of Interest

Treating resource file as an XML File makes our editing easier than it is.

Conclusion

I am sorry for the poor formatting and explanation. I just wanted to show you a simple way of editing a resource file and that's it. I will appreciate any feedback. Expect more from me soon!

History

  • 11th April, 2007: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionASP.NET MVC 5 example Pin
alpay3513-Sep-22 11:07
alpay3513-Sep-22 11:07 
QuestionWhat About Session state Pin
faizkazi4u11-Nov-12 21:26
faizkazi4u11-Nov-12 21:26 
QuestionSession expires Pin
Member 29264189-Sep-12 21:23
Member 29264189-Sep-12 21:23 
AnswerRe: Session expires Pin
faizkazi4u11-Nov-12 21:29
faizkazi4u11-Nov-12 21:29 
AnswerEditing a Resource File Treating It as an XML File Pin
adeel8420-Sep-11 21:44
adeel8420-Sep-11 21:44 
GeneralThe directory '/App_GlobalResources/' is not allowed because the application is precompiled. Pin
dailywake17-Mar-10 7:31
dailywake17-Mar-10 7:31 
GeneralSame project failed to work when published using 'Publish Web Site' utility (VS2005) Pin
Member 57974298-May-09 5:28
Member 57974298-May-09 5:28 
QuestionEditing after Publishing Web site Pin
Member 355227012-Dec-07 1:02
Member 355227012-Dec-07 1:02 
GeneralEditing Resource file as XML File [modified] Pin
Ganesan Sankaran18-Oct-07 21:27
Ganesan Sankaran18-Oct-07 21:27 
QuestionHow to give the column name for the Bind Column in the Grid Pin
Ganesan Sankaran18-Oct-07 0:20
Ganesan Sankaran18-Oct-07 0:20 
GeneralCould not find a part of the path 'C:\TestApp\App_GlobalResources\'. Pin
Sc3pt1c4l30-Aug-07 5:40
Sc3pt1c4l30-Aug-07 5:40 
QuestionDoes this matter? Pin
E L N I N O16-Jul-07 20:46
E L N I N O16-Jul-07 20:46 
AnswerRe: Does this matter? Pin
shahbaz rehman9-Nov-08 23:50
shahbaz rehman9-Nov-08 23:50 
Generalask a question Pin
Databinder12-Apr-07 16:54
Databinder12-Apr-07 16:54 
QuestionPrevious article? Pin
Mahesh Sapre11-Apr-07 20:18
Mahesh Sapre11-Apr-07 20:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.