Click here to Skip to main content
Licence CPOL
First Posted 11 Apr 2007
Views 32,888
Downloads 477
Bookmarked 20 times

Editing a Resource File Treating It as an XML File

By | 11 Apr 2007 | Article
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:

<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.

<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:

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.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:

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.

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

About the Author

Saleth Prakash



India India

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
AnswerEditing a Resource File Treating It as an XML File Pinmemberadeel8421:44 20 Sep '11  
GeneralThe directory '/App_GlobalResources/' is not allowed because the application is precompiled. Pinmemberdailywake7:31 17 Mar '10  
GeneralSame project failed to work when published using 'Publish Web Site' utility (VS2005) PinmemberMember 57974295:28 8 May '09  
QuestionEditing after Publishing Web site PinmemberMember 35522701:02 12 Dec '07  
GeneralEditing Resource file as XML File [modified] PinmemberGanesan Sankaran21:27 18 Oct '07  
QuestionHow to give the column name for the Bind Column in the Grid PinmemberGanesan Sankaran0:20 18 Oct '07  
GeneralCould not find a part of the path 'C:\TestApp\App_GlobalResources\'. PinmemberW Charlton5:40 30 Aug '07  
QuestionDoes this matter? Pinmember[ELNINO]20:46 16 Jul '07  
AnswerRe: Does this matter? Pinmembershahbaz rehman23:50 9 Nov '08  
Generalask a question PinmemberDatabinder16:54 12 Apr '07  
QuestionPrevious article? PinmemberMahesh Sapre20:18 11 Apr '07  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 12 Apr 2007
Article Copyright 2007 by Saleth Prakash
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid