GetEnumerator Method
Collapse the table of content
Expand the table of content

ResXResourceReader.GetEnumerator Method ()

 

Returns an enumerator for the current ResXResourceReader object.

Namespace:   System.Resources
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public IDictionaryEnumerator GetEnumerator()

Return Value

Type: System.Collections.IDictionaryEnumerator

An enumerator for the current ResourceReader object.

The GetEnumerator method retrieves the name/value pairs in the XML resource (.resx) stream or string associated with the current ResXResourceReader object. However, if the UseResXDataNodes property is set to true before you call GetEnumerator, the resource items are retrieved as ResXDataNode objects. In this case, all resource nodes are returned regardless of type.

The following example uses the GetEnumerator method to obtain an IDictionaryEnumerator object that is used to enumerate the resources in a .resx file. The example includes a CreateResourceFile routine that creates the necessary resource file.

using System;
using System.Collections;
using System.Resources;

public class Example
{
   private const string resxFilename = @".\CountryHeaders.resx";

   public static void Main()
   {
      // Create a resource file to read.
      CreateResourceFile();

      // Enumerate the resources in the file.
      ResXResourceReader rr = new ResXResourceReader(resxFilename);
      IDictionaryEnumerator dict = rr.GetEnumerator();
      while (dict.MoveNext())
         Console.WriteLine("{0}: {1}", dict.Key, dict.Value);   
   }

   private static void CreateResourceFile()
   {
      ResXResourceWriter rw = new ResXResourceWriter(resxFilename);
      string[] resNames = {"Country", "Population", "Area", 
                           "Capital", "LCity" };
      string[] columnHeaders = { "Country Name", "Population (2010}", 
                                 "Area", "Capital", "Largest City" };
      string[] comments = { "The localized country name", "", 
                            "The area in square miles", "", 
                            "The largest city based on 2010 data" };
      rw.AddResource("Title", "Country Information");
      rw.AddResource("nColumns", resNames.Length);
      for (int ctr = 0; ctr < resNames.Length; ctr++) {
         ResXDataNode node = new ResXDataNode(resNames[ctr], columnHeaders[ctr]);
         node.Comment = comments[ctr];
         rw.AddResource(node);
      }
      rw.Generate();
      rw.Close();
   }
}
// The example displays the following output:
//       Title: Country Information
//       nColumns: 5
//       Country: Country Name
//       Population: Population (2010}
//       Area: Area
//       Capital: Capital
//       LCity: Largest City

.NET Framework
Available since 1.1
Return to top
Show:
© 2016 Microsoft