Click here to Skip to main content
15,178,202 members
Articles / Web Development / ASP.NET
Posted 11 Apr 2007


25 bookmarked

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
This demo shows how to edit a resource file content by treating it as an XML File


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:

<data name="Language" xml:space="preserve">
<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.

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

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:

using System.Globalization;
using System.Resources;
using System.IO;

Then, in the page load event:

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.Insert(0, new ListItem("Select a Resource File"));

The output will be:

Screenshot - output1.jpg

Then, in the Dropdownlist SelectedIndexChanged event:

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);
            gridView1.DataSource = slist;

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.

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:

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();
        XmlNodeList nlist = xmlDoc.GetElementsByTagName("data");
        XmlNode childnode = nlist.Item(id);
        childnode.Attributes["xml:space"].Value = "default";               
        XmlNode lastnode = childnode.SelectSingleNode("value");
        lastnode.InnerText = txtResourceValue.Text;
        Label2.Text = "Resource File Updated...";

Points of Interest

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


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!


  • 11th April, 2007: Initial post


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


About the Author

No Biography provided

Comments and Discussions

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