Page view counter

Getting resource name

Last post 04-20-2009 11:25 AM by joniba. 4 replies.

Sort Posts:

  • Getting resource name

    10-04-2007, 4:04 PM
    • Loading...
    • MADCookie
    • Joined on 02-20-2003, 9:43 AM
    • Posts 8
    • Points 2

    Is there a way to loop through the items in a resource file so I can get the item's name and value?

     

  • Re: Getting resource name

    10-04-2007, 5:16 PM
    Answer
    • Loading...
    • jamlew
    • Joined on 09-21-2007, 3:43 PM
    • Redmond, WA
    • Posts 28
    • Points 136

    Resource files are just XML, where the important part is a series of  

    <data name="resource name"><value>localized string</value></data>
       (though there is a little bit of other stuff).

    Here's a sample of parsing it with C# 2.0 (hopefully I did this right, I haven't worked with XML much):

            Dictionary<string, string> resourceDict = new Dictionary<string,string>();
            XmlDocument doc = new XmlDocument();
            doc.Load(Server.MapPath("~/App_GlobalResources/Resources.resx"));
            XmlNodeList resourceList = doc.GetElementsByTagName("data");
            foreach (XmlNode node in resourceList)
            {
                string name = node.Attributes["name"].Value;
                string value  = ""; // fallback value if for some reason it doesn't have a value
                foreach (XmlNode childNode in node.ChildNodes)
                    if (childNode.Name.Equals("value"))
                    {
                        value = childNode.InnerText;
                        break;
                    }
    
                resourceDict.Add(name, value);
            }
     If you're using C# 3.0, this becomes a lot easier:
     var resourceDict = from n in XElement.Load(Server.MapPath("~/App_GlobalResources/Resources.resx")).Elements("data")

    select new { Name = (string)n.Attribute("name"), Value = (string)n.Element("value") };

     
    ---------
    If this post has solved your problem, please select 'Mark as answer'

    - Jimmy Lewis
    Microsoft SDET
    Visual Web Developer Team
    Silverlight Tools for VS
  • Re: Getting resource name

    04-20-2009, 6:02 AM
    • Loading...
    • joniba
    • Joined on 06-12-2007, 8:27 AM
    • Posts 17
    • Points 20

     Good God, isn't there a way to do this without running to the Xml itself?  Like with the ResourceSet or something?

  • Re: Getting resource name

    04-20-2009, 6:19 AM
    • Loading...
    • mnemukula
    • Joined on 02-13-2008, 10:36 AM
    • South Africa
    • Posts 21
    • Points 56

     Hi there,

    try using something like this: Resources.YourResourceName.Name this will give you the value of the name you've selected.

    please mark as correct if this helps you get your question right

     

    Nemukula Mashudu
  • Re: Getting resource name

    04-20-2009, 11:25 AM
    • Loading...
    • joniba
    • Joined on 06-12-2007, 8:27 AM
    • Posts 17
    • Points 20

     Actually my proposed solution, after some research, to the original question, is:

     

     

    /// &lt;summary&gt;
    /// Returns a dictionary of the name/value pairs of the managed resource-file
    /// </summary>

    public static Dictionary<string, string> BuildResourceDictionary(ResourceManager manager)
    {
    Dictionary<string, string> resourceDict = new Dictionary<string, string>();

    ResourceSet resourceSet = manager.GetResourceSet(CultureInfo.CurrentUICulture, true, true);

    IDictionaryEnumerator enumerator = resourceSet.GetEnumerator();

    while (enumerator.MoveNext())
    {
    resourceDict.Add((string)enumerator.Key, (string)enumerator.Value);
    }

    return resourceDict;
    }
     
    Though I implemented it as an extension method.
    Of course you could also add a CultureInfo parameter rather than use the CurrentUICulture. 
      
Page 1 of 1 (5 items)