0

My Global Resx file named Appointment.resx

enter image description here

I'm accessing the Key Values in c# using the following code:

 string str = Resources.Appointment.AppointmentID;

Now, my issue is how to get the Comments value using the key.

Note: Comments is nothing but a column in resx file.

Please help. Thanks.

Pearl
  • 8,373
  • 8
  • 40
  • 59
  • Just wondering why you would want to use the Comments field? It is suppose to be a reference to the Key so when you look at the comment it should tell you about the key. e.g. where it's being used. – Jamie Rees Aug 06 '15 at 13:08

2 Answers2

0

You should be able to get Comment via ResXDataNode class: http://msdn.microsoft.com/en-us/library/system.resources.resxdatanode.aspx

This is code:

// Enumerate the resources in the file.
      ResXResourceReader rr = new ResXResourceReader(resxFilename);
      rr.UseResXDataNodes = true;
      IDictionaryEnumerator dict = rr.GetEnumerator();
      while (dict.MoveNext()) {
         ResXDataNode node = (ResXDataNode) dict.Value;
         Console.WriteLine("{0,-20} {1,-20} {2}", 
                           node.Name + ":", 
                           node.GetValue((ITypeResolutionService) null), 
                           ! String.IsNullOrEmpty(node.Comment) ? "// " + node.Comment : "");
      }

You will need to set UseResXDataNodes flag on the reader: http://msdn.microsoft.com/en-us/library/system.resources.resxresourcereader.useresxdatanodes.aspx

But NOTE - This way seems to work ONLY for .RESX file on disk.

David Abaev
  • 690
  • 5
  • 22
0

I think it should be possible with the ResXResourceReader.

ResXResourceReader rr = new ResXResourceReader(resxFilename);
rr.UseResXDataNodes = true; // this is important!

var resXDataNodes = rr.GetEnumerator().Select(i => i.Value).OfType<ResXDataNode>();

foreach(var resXDataNode in resXDataNodes)
{
   var comment = resXDataNode.Comment;
}
CodeTherapist
  • 2,776
  • 14
  • 24