Click here to Skip to main content
14,830,989 members
Articles / Web Development / ASP.NET
Article
Posted 11 Apr 2007

Stats

87.9K views
1.8K downloads
26 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

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)

Share

About the Author

No Biography provided

Comments and Discussions

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