locked
Getting resource name RRS feed

  • Question

  • User2113362401 posted

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

     

    Thursday, October 4, 2007 4:04 PM

Answers

  • User1642585364 posted

    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") };

     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 4, 2007 5:16 PM

All replies

  • User1642585364 posted

    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") };

     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 4, 2007 5:16 PM
  • User-1912779769 posted

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

    Monday, April 20, 2009 6:02 AM
  • User142102346 posted

     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

     

    Monday, April 20, 2009 6:19 AM
  • User-1912779769 posted

     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. 
      
    Monday, April 20, 2009 11:25 AM