0 out of 2 rated this helpful - Rate this topic

ResXResourceReader Class

Enumerates XML resource (.resx) files and streams, and reads the sequential resource name and value pairs.

System.Object
  System.Resources.ResXResourceReader

Namespace:  System.Resources
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
[PermissionSetAttribute(SecurityAction.InheritanceDemand, Name = "FullTrust")]
[PermissionSetAttribute(SecurityAction.LinkDemand, Name = "FullTrust")]
public class ResXResourceReader : IResourceReader, 
	IEnumerable, IDisposable

The ResXResourceReader type exposes the following members.

  Name Description
Public method ResXResourceReader(Stream) Initializes a new instance of the ResXResourceReader class for the specified stream.
Public method ResXResourceReader(String) Initializes a new instance of the ResXResourceReader class for the specified resource file.
Public method ResXResourceReader(TextReader) Initializes a new instance of the ResXResourceReader class for the specified TextReader.
Public method ResXResourceReader(Stream, ITypeResolutionService) Initializes a new instance of the ResXResourceReader class using an input stream and a type resolution service.
Public method ResXResourceReader(Stream, AssemblyName[]) Initializes a new instance of the ResXResourceReader class using a stream and an array of assembly names.
Public method ResXResourceReader(String, ITypeResolutionService) Initializes a new instance of the ResXResourceReader class using a file name and a type resolution service.
Public method ResXResourceReader(String, AssemblyName[]) Initializes a new instance of the ResXResourceReader class using an XML resource file name and an array of assembly names.
Public method ResXResourceReader(TextReader, ITypeResolutionService) Initializes a new instance of the ResXResourceReader class using a text stream reader and a type resolution service.
Public method ResXResourceReader(TextReader, AssemblyName[]) Initializes a new instance of the ResXResourceReader class using a TextReader object and an array of assembly names.
Top
  Name Description
Public property BasePath Gets or sets the base path for the relative file path specified in a ResXFileRef object.
Public property UseResXDataNodes Gets or sets a value indicating whether ResXDataNode objects are returned when reading the current XML resource file or stream.
Top
  Name Description
Public method Close Releases all resources used by the ResXResourceReader.
Protected method Dispose Releases the unmanaged resources used by the ResXResourceReader and optionally releases the managed resources.
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize This member overrides the Finalize method. (Overrides Object.Finalize().)
Public method Static member FromFileContents(String) Creates a new ResXResourceReader object and initializes it to read a string whose contents are in the form of an XML resource file.
Public method Static member FromFileContents(String, ITypeResolutionService) Infrastructure. Creates a new ResXResourceReader object and initializes it to read a string whose contents are in the form of an XML resource file, and to use an ITypeResolutionService object to resolve type names specified in a resource.
Public method Static member FromFileContents(String, AssemblyName[]) Infrastructure. Creates a new ResXResourceReader object and initializes it to read a string whose contents are in the form of an XML resource file, and to use an array of AssemblyName objects to resolve type names specified in a resource.
Public method GetEnumerator Returns an enumerator for the current ResXResourceReader object.
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetMetadataEnumerator Provides a dictionary enumerator that can retrieve the design-time properties from the current XML resource file or stream.
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method ToString Returns a string that represents the current object. (Inherited from Object.)
Top
  Name Description
Public Extension Method AsParallel Enables parallelization of a query. (Defined by ParallelEnumerable.)
Public Extension Method AsQueryable Converts an IEnumerable to an IQueryable. (Defined by Queryable.)
Public Extension Method Cast<TResult> Casts the elements of an IEnumerable to the specified type. (Defined by Enumerable.)
Public Extension Method OfType<TResult> Filters the elements of an IEnumerable based on a specified type. (Defined by Enumerable.)
Top
  Name Description
Explicit interface implemetation Private method IDisposable.Dispose Infrastructure. Releases the unmanaged resources used by the ResXResourceReader and optionally releases the managed resources. For a description of this member, see IDisposable.Dispose.
Explicit interface implemetation Private method IEnumerable.GetEnumerator Infrastructure. Returns an enumerator for the current ResXResourceReader object. For a description of this member, see IEnumerable.GetEnumerator.
Top

The ResXResourceReader provides a default implementation of the IResourceReader interface that reads resource information in an XML format. To read resource information from a binary resource format, use ResourceReader.

You can use resource readers to read resource name and value pairs from .resx files. The resources can be enumerated by traversing the IDictionaryEnumerator returned by the GetEnumerator method. You can use the methods provided by the IDictionaryEnumerator to advance to the next resource and read the name and value of each resource in the .resx file.

For more information on the format of a .resx file, see Resources in .resx File Format.

Note Note

This class contains a link demand and an inheritance demand at the class level that applies to all members. A SecurityException is thrown when either the immediate caller or the derived class does not have full-trust permission. For details about security demands, see Link Demands and Inheritance Demands.

The following example demonstrates how to use a ResXResourceReader to iterate through the resources in a .resx file. First, the ResXResourceReader rsxr is created for the file items.resx. Next, the GetEnumerator method is used to create an IDictionaryEnumerator to iterate through the resources and display the contents to the console.


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

class ReadResXResources
{
   public static void Main()
   {

      // Create a ResXResourceReader for the file items.resx.
      ResXResourceReader rsxr = new ResXResourceReader("items.resx");

      // Iterate through the resources and display the contents to the console.
      foreach (DictionaryEntry d in rsxr)
      {
    Console.WriteLine(d.Key.ToString() + ":\t" + d.Value.ToString());
      }

     //Close the reader.
     rsxr.Close();
   }
}


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Good help as it should be...

Although it helped me but i want to ask one question. Is there any way to pick the files on the basis of locale? I dont want to give the physical file name for other localized resource files.

Getting Files on the Basis of Locale


If you've embedded binary .resources files in assemblies, the .NET Framework handles the details of loading the appropriate resources for the current culture, or you can load resources for a specific culture. The ResXResourceReader class, on the other hand, reads resources from individual .resx (XML resources) files and doesn't provide support for automatically loading a particular set of localized resources, such as those belonging to the current culture. If you want to load a particular localized .resx file, you have to specify its filename in the ResXResourceReader class constructor. If you've followed the convention of including the locale name in the filename, you could use code like the following:

string resxFilename  = "resfile." + CultureInfo.CurrentCulture.Name + ".resx"


If you want to automatically retrieve the resources of the current culture without having to specify the physical name of the resource file, the best alternative is to embed the resources in an assembly and use the ResourceManager class to retrieve the resources. For details, see the Creating Resources Files topic in the MSDN documentation at http://msdn.microsoft.com/en-us/library/xbx3z216.aspx.

I hope that this helps.

--Ron Petrusha
Common Language Library Developer Content
Microsoft Corporation