0

I would like to get the description field in a .resx field using C#, At the moment I can get the "value" field using:

public static String f_str_textoRecurso(String p_str_archivo, String p_str_key)
{
    System.Resources.ResourceManager t_rsm = 
      new System.Resources.ResourceManager("Resources." + p_str_archivo,
        System.Reflection.Assembly.Load("App_GlobalResources"));

    String t_str = t_rsm.GetString(p_str_key);
    if (t_str != null)
    {
      if (p_str_key.Equals(""))
      {
        t_str = p_str_archivo.Remove(0, 4) + "." + p_str_key; 
      } 
    }
    else
    {
      t_str = p_str_archivo.Remove(0, 4) + "." + p_str_key; 
    }
    return t_str;
}

But I need also get comment. Any ideas?

enter image description here

enter image description here

Aleks Andreev
  • 7,016
  • 8
  • 29
  • 37
krlosruiz
  • 113
  • 3
  • 11
  • check https://msdn.microsoft.com/en-us/library/system.resources.resxdatanode.aspx – Mate Mar 20 '18 at 15:56
  • Unfortunately simply **you cannot**. Compiled .resource files do not contain the _description_ field you have in the original .resx file. If you're trying to do this to generate user-friendly documents (for translators, for example) then you should work with .resx files. – Adriano Repetti Mar 20 '18 at 15:57
  • You cannot get it from ResourceManager, just like you can't get a // comment from source code. You can get it from ResXResourceReader but shipping .resx files is not normal. Surely there is a better way to achieve what you want, but you can't get a good answer when you don't explain why. – Hans Passant Mar 20 '18 at 15:58
  • 1
    Possible duplicate of [How to get the comment from a .resx file entry](https://stackoverflow.com/questions/8854404/how-to-get-the-comment-from-a-resx-file-entry) – Mate Mar 20 '18 at 15:58
  • Thanks everyone! I decided to abort mission and find other alternative – krlosruiz Mar 21 '18 at 13:30

1 Answers1

1

Here you go:

    public string ReadResourceComment(XmlDocument doc, string FieldName)
    {
        if (doc != null && !string.IsNullOrEmpty(doc.InnerXml))
        {
            return doc.SelectSingleNode("root/data[@name='" + FieldName + "']")["comment"].InnerText;
        }

        return string.Empty;
    }

Here is how to use it:

  1. Read your file ( It's XML document )
  2. Pass which node you wanna read its comment

Example:

        XmlDocument doc = new XmlDocument();
        string filePath = HttpContext.Current.Server.MapPath("~/[FileName].resx");
                    doc.Load(filePath);

        string comment = ReadResourceComment(doc, "[NodeName]");
        // In your case, use ( ReadResourceComment(doc, "ot_desdecontrato");
Khaled Saleh
  • 116
  • 1
  • 9