Once in a day, when I need to read all resx file contents and put all of them into database I tried to find resource with googling but I couldn't find any proper solution to read efficiently so I tried to create a method in a basic way of xml reading.
To read all key and values from .net resource language map files. Following method can be used :
private static Dictionary<string, string> GetResourceFile(string languageCode)
{
Dictionary<string, string> langMap = new Dictionary<string, string>();
string fileName = $"SharedResource.{languageCode}.resx";
string fullPath = "D:\\projects\\projectName\\Resources\\" + fileName;
XDocument xDoc = XDocument.Load(fullPath);
foreach (XElement item in xDoc.Root.Elements("data"))
{
string key = item.Attribute("name").Value;
string value = item.Element("value").Value;
langMap.Add(key, value);
}
return langMap;
}
(In my case, there are two resource files into Resources folder in root of project as SharedResource.en-US.resx and SharedResource.tr-TR.resx. to read one of them just pass input as en-US or tr-TR and get dictionary of key value map.)