【问题标题】:How to get comment or description field .resx file C#如何获取评论或描述字段.resx文件C#
【发布时间】:2018-03-20 15:46:13
【问题描述】:

我想使用 C# 获取 .resx 字段中的描述字段,目前我可以使用以下方法获取“值”字段:

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

但我还需要得到评论。有什么想法吗?

【问题讨论】:

  • 很遗憾你不能。编译的 .resource 文件不包含原始 .resx 文件中的 description 字段。如果您尝试这样做以生成用户友好的文档(例如,对于翻译人员),那么您应该使用 .resx 文件。
  • 您无法从 ResourceManager 中获取它,就像您无法从源代码中获取 // 注释一样。您可以从 ResXResourceReader 获取它,但传送 .resx 文件是不正常的。当然有更好的方法来实现你想要的,但是如果你不解释原因,你就无法得到一个好的答案。
  • 谢谢大家!我决定中止任务并寻找其他替代方案

标签: c# resx resourcemanager


【解决方案1】:

给你:

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

这里是如何使用它:

  1. 读取您的文件(它是 XML 文档)
  2. 传递您要阅读其评论的节点

例子:

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

【讨论】:

    猜你喜欢
    • 2012-02-09
    • 1970-01-01
    • 2019-01-06
    • 2021-10-19
    • 2022-06-16
    • 1970-01-01
    • 2017-08-14
    • 2016-12-18
    相关资源
    最近更新 更多