65.9K
CodeProject is changing. Read more.
Home

Editing a Resource File Treating It as an XML File

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.44/5 (7 votes)

Apr 12, 2007

CPOL

1 min read

viewsIcon

97169

downloadIcon

1847

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