locked
Writing Editing Reading Resource .resx files In web Based application asp.net 2.0 C# RRS feed

  • Question

  • User456878850 posted

    Hey,

     I have been reading for quiet a while now without finding an solution. I bassicly want to read, write and edit my resource files in xml .resx format.

    Now my first try was something like this:

    Assembly ass = Assembly.Load("App_GlobalResources");

    ResourceManager resourcer = new ResourceManager("Resource", ass);

    Languagelbl.Text = resourcer.GetString(Session["Language"].ToString());

    With Resouce.resx as the resource name I want to connect to. But I read a couple of minutes later that this approache is not possible for web based application because it generates a randem dll.

     

    ResourceManager resourcer = new ResourceManager("Website3.Resource ", Assembly.GetExecutingAssembly());

    string AddLanguageName = LanguageNamelbl.Text;

    string AddLanguageText = LanguageTextlbl.Text;

    ResourceWriter writer = new ResourceWriter("resourcer");

    writer.AddResource("Name", "Text");

    writer.Close();

    With Website3 as the temporary name for the web application and Resource as the resource I want to reach. But this doesent seem to work either. Maybe because web applications dont have a "real" name?

    Later I read that I need resxresourcewriter to write into an .resx file, but for some reason I cant use it. Add least its not on my list. Some one said something like you need windows.system.resource or something for resxresource namespaces.

     

    I hope you understand that I get a bit confused with all this information. I will keep searching but I hope you can atleast explain why I cant use my previous approaches, or maybe some one even knows a solution for this.

    Thank you in advanced,

    Kevin Hendricks,

    Wednesday, August 29, 2007 5:09 AM

All replies

  • User456878850 posted

    I have been working on my solution for the past 8 hours now.

    I am currently this far:

     

    protected void Page_Load(object sender, EventArgs e)

    {

    if (!this.IsPostBack)

    {

    ResXResourceReader reader = new ResXResourceReader(Server.MapPath("~/App_GlobalResources/Resource.resx"));

    IDictionaryEnumerator id = reader.GetEnumerator();

    DataTable dt = new DataTable();

    dt.Columns.Add(new DataColumn("Name", System.Type.GetType("System.String")));

    dt.Columns.Add(new DataColumn("Value", System.Type.GetType("System.String")));

     

    foreach (DictionaryEntry d in reader)

    {

    DataRow dr = dt.NewRow();

    dr["Name"] = d.Key.ToString();

    dr["Value"] = d.Value.ToString();

    dt.Rows.Add(dr);

    }

    reader.Close();

    LanguageDropdownlist.DataSource = dt;

    LanguageDropdownlist.DataValueField =
    "Name";LanguageDropdownlist.DataTextField = "Value";

    LanguageDropdownlist.DataBind();

     

    }

    }

    protected void Language_Click(object sender, EventArgs e)

    {

    ResourceNamelbl.Text = LanguageDropdownlist.SelectedValue;

    ResourceValue.Text = LanguageDropdownlist.SelectedItem.Text;

    }

    protected void AddingLanguage_Click(object sender, EventArgs e)

    {

    string Name = ResourceNamelbl.Text;

    string ModifiedValue = ResourceValue.Text;

    ResXResourceWriter writer = new ResXResourceWriter(Server.MapPath("~/App_GlobalResources/Resource.resx"));

    writer.Generate();

    writer.Close();

    }

     

    The only problem I now have is that I need to modify the data. There are 2 possibilitys but I dont know how to make one of them so I am still reading through the internet.

    The first one would be to just modify the data directly so something like inserting where name = name.

    The second would be that I make a public datatable and create a new file with a loop till its done and a if statement for if i get the name of the modified name so I can modify the default.

     

    Does any one have an solution or an idear?

    With kind regards,

    Kevin Hendricks.

    Wednesday, August 29, 2007 11:43 AM
  • User889692731 posted

    Did you ever get this working or does anyone know how to do this?

    Tuesday, January 15, 2008 6:51 AM
  • User456878850 posted

    I actually did get it to work. The problem is that the resx reader can not read the comment. and after alot of research and alot of testing I came with the following solution.

    I use an XML reader to read the resource by looping. It is difficult but I got it to work. Then on each edit I write the whole resource with an text writer.

    I cannot send you the modified files wich I put into an existing website.  but I can email you the older files wich DO WORK. So please email me to biodoom@gmail.com I will reply you the files you can use as an example. You can ask me if you have any qeustions.


    With kind regards,

    Kevin Hendricks. 

    Tuesday, January 15, 2008 7:03 AM
  • User889692731 posted

     Thanks, I have emailed you.

     I did read somewhere that you should use satellite assemblies for better performance but I'm not really sure what is right in this process.

    Tuesday, January 15, 2008 7:16 AM
  • User889692731 posted

     Thanks for you code you provided, looks pretty in depth.

    I have written this code which you can have which is pretty simple.

    Hope it helps, any comments then feel free to post them.

     

    	    XmlDocument loResource = new XmlDocument();
                loResource.Load(Server.MapPath("/App_GlobalResources/TDLResources.de-DE.resx"));
    
                XmlNode loRoot = loResource.SelectSingleNode("root/data[@name='RequiredFields']/value");
                      
                if (loRoot != null)
                {
                    loRoot.InnerText = "test";
                    loResource.Save(Server.MapPath("/App_GlobalResources/TDLResources.de-DE.resx"));
                }
      
    Tuesday, January 15, 2008 7:46 AM
  • User456878850 posted

    So you can edit a single value with the code you made? Looks good.

    It was the first thing I ever developt so it can still have alot of improvement. 

     

    Thx for the reply.

    With kind regrads,

    Kevin Hendricks.
     

    Tuesday, January 15, 2008 8:01 AM
  • User59278684 posted

    Hi Kevin,

    I am trying to write,edit and read resource files... Now I dont know how to edit/write to a resource file.It would be great if u could help me out...Since u have already done this..Thanx a lot....

    I have done this much... 

    ResXResourceReader
    reader = new ResXResourceReader(Server.MapPath("~/App_LocalResources/"+lstresource.SelectedItem.Text));

     

     

    DataTable dt = new DataTable();

    dt.Columns.Add(new DataColumn("Name", System.Type.GetType("System.String")));

    dt.Columns.Add(new DataColumn("Value", System.Type.GetType("System.String")));

     

    foreach (DictionaryEntry d in reader)

    {

    DataRow dr = dt.NewRow();

    dr["Name"] = d.Key.ToString();

    dr["Value"] = d.Value.ToString();

    dt.Rows.Add(dr);

    }

    reader.Close();

    this.grddisplay.Visible = true;

    grddisplay.DataSource = dt ;

    grddisplay.DataBind();

     

    Tuesday, July 15, 2008 1:08 AM
  • User-1464423983 posted

     Thanks for you code you provided, looks pretty in depth.

    I have written this code which you can have which is pretty simple.

    Hope it helps, any comments then feel free to post them.

     

    	    XmlDocument loResource = new XmlDocument();
                loResource.Load(Server.MapPath("/App_GlobalResources/TDLResources.de-DE.resx"));
    
                XmlNode loRoot = loResource.SelectSingleNode("root/data[@name='RequiredFields']/value");
                      
                if (loRoot != null)
                {
                    loRoot.InnerText = "test";
                    loResource.Save(Server.MapPath("/App_GlobalResources/TDLResources.de-DE.resx"));
                }

      

    awesome .. it helped me.

    Tuesday, December 22, 2009 3:10 PM
  • User774139589 posted

    Just replace 'Resource' with 'Resources.Resource'...

    Assembly ass = Assembly.Load("App_GlobalResources");

    ResourceManager resourcer = new ResourceManager("Resources.Resource", ass);

     Languagelbl.Text = resourcer.GetString(Session["Language"].ToString());

    .net framework keep this 'Resource.resx' file under Resources namespace with class name as Resource

    Thursday, January 14, 2010 5:01 AM
  • User-2060258345 posted

    Sorry i'm a newbie .. but c# make an error on Server.MapPath :/

    Tuesday, March 9, 2010 5:25 AM
  • User456878850 posted

    Server.MapPath is a asp.net application method. Are you making a webapplication or a desktop application?

    Tuesday, March 9, 2010 6:03 AM
  • User-2060258345 posted

    application desktop ... didnt used this finally .. thanks for ur comeback

    Thursday, March 11, 2010 5:42 AM
  • User-1481646738 posted

    Thanks a lot ,

    Nice solution :)

    Monday, November 22, 2010 4:10 AM
  • User1368300014 posted

    actually i need to include the name/value pair to .resx file(which is already created)

    i cant add it. whenever i add the existing name/value pairs get deleted and the one which is added at last is present.

    i have used below code

    ResXResourceWriter resourceWriter = new ResXResourceWriter(fileinfo.FullName);

    resourceWriter.AddResource("key1", "from extensibility code45");

    resourceWriter.Generate();

    resourceWriter.Close();

    plx may i know any other way of incudeing resource to .resx file.

    Thursday, June 23, 2011 8:10 AM
  • User1368300014 posted

    HI all

    actually i have done it by reading .resx file as XML and adding the new node to it.

    which get updated to .resx file.

    thanks alot to all for their valuable solutions.

     

    thanks

    Wednesday, June 29, 2011 1:14 AM