Introduction to The Resources .resx and Resources Files: Part I

Introduction

In some cases an application needs some external resources to perform specified tasks. And I mean by external resources, those none executables data logically deployed with a given application. The final purpose by doing so is to prevent recompiling the given application for each time one or more elements are supposed to be necessarily changed according to some environmental exceptions, contexts or external conditions. You tell me OK understood, but the same task or mission is covered by the configuration files. I can say resources files and configuration files exist for the same goal is to prevent recompiling applications, the nature and the mission covered by each kind of file differs in practice, however.

Configuration files "*.config" vs. resource files "*.resource"

The configuration files "*.exe.config" have as a mission giving the developer the ability to control and/ or modify settings inside the application logical environment, among the missions covered by the "*.exe.config" files:

  • Define witch assemblies could be consumed by the application core.
  • Specify witch runtime version processes.
  • Define application settings such as connection strings and other settings.
  • Register remote objects
  • Define some configuration sections especially used to certain properties assignments

So, each of those elements belongs logically to the internal application environment. In the other hand, the resources files "*.resource" are designed to provide information contained in a tierce parts those belong to the external logical application environment such as bitmaps images, text files, icons and so forth.

Untitled-1.gif

You can save those files types most used as a part of your resources:

Open file as Save file as Description
32-bit .res .rc or 32-bit .res The famous (*.rc) files used in VC++6.0 if you have some backgrounds according to C++ programming, the .rc files are added automatically within a VC++ application as resources files
.bmp or .dib .bmp or .dib Bitmaps image files and device independent bitmap image files
.ico .ico Icon files
.cur .cur A type of specified Icon files used to design cursors
.htm, .html .htm or .html The html files

Resx files vs. resources files

The question now, is why there are two different formats, I mean, resx and resource format or extension to represent the same kind of files, namely, the resource files. And why, for each time, that I use the resx format file, I encounter problems to embed it in a run time executable environment.

Well, for the first question I can say that there is a difference in nature between a (*.resx) and (*.resource) file.

Resx file:

The first one is a kind of structured XML format file, such as the XSD files those used to stock information about datasets elements and structures. It is, normally, used for structuring and organizing data in a given order. Within a resx file you can add, modify or delete given information about resources through the code or even by using a simple text editor if you have a strong background concerning the XML files handling and, of Corse, good knowledge according to the resx files elements and structure. It is possible to use a text file instead of the resx file for the same purpose, but it should be better to use the last one. In addition, it is not a good idea to store sensitive information such as passwords, visa cards data or personal data in a resx file as they can be easily seen by everyone who has access to it.

And this is a resx file example in which I stocked my name and my country in string variables; I give this example to see how such file can look like:

<?xml version="1.0" encoding="utf-8"?>

<root>

  <!--

    Microsoft ResX Schema Version 2.0
   

    The primary goals of this format is to allow a simple XML format that is mostly human readable. The generation and parsing of the various data types are done through the TypeConverter classes associated with the data types.

    Example:
   

    ... ado.net/XML headers & schema ...

    <resheader name="resmimetype">text/microsoft-resx</resheader>

    <resheader name="version">2.0</resheader>

    <resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>

    <resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>

    <data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>

    <data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>

    <data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">

        <value>[base64 mime encoded serialized .NET Framework object]</value>

    </data>

    <data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">

        <value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>

        <comment>This is a comment</comment>

    </data>
               

    There are any number of "resheader" rows that contain simple name/value pairs.

   

    Each data row contains a name, and value. The row also contains a type or mimetype. Type corresponds to a .NET class that support text/value conversion through the TypeConverter architecture.Classes that don't support this are serialized and stored with the mimetype set.
   

    The mimetype is used for serialized objects, and tells the ResXResourceReader how to depersist the object. This is currently not extensible. For a given mimetype the value must be set accordingly:
   

    Note - application/x-microsoft.net.object.binary.base64 is the format that the ResXResourceWriter will generate, however the reader can read any of the formats listed below.
   

    mimetype: application/x-microsoft.net.object.binary.base64

    value: The object must be serialized with

            : System.Runtime.Serialization.Formatters.Binary.BinaryFormatter

            : and then encoded with base64 encoding.

   

    mimetype: application/x-microsoft.net.object.soap.base64

    value   : The object must be serialized with

            : System.Runtime.Serialization.Formatters.Soap.SoapFormatter

            : and then encoded with base64 encoding.

 

    mimetype: application/x-microsoft.net.object.bytearray.base64

    value   : The object must be serialized into a byte array

            : using a System.ComponentModel.TypeConverter

            : and then encoded with base64 encoding.

    -->

  <xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">

    <xsd:import namespace="http://www.w3.org/XML/1998/namespace" />

    <xsd:element name="root" msdata:IsDataSet="true">

      <xsd:complexType>

        <xsd:choice maxOccurs="unbounded">

          <xsd:element name="metadata">

            <xsd:complexType>

              <xsd:sequence>

                <xsd:element name="value" type="xsd:string" minOccurs="0" />

              </xsd:sequence>

              <xsd:attribute name="name" use="required" type="xsd:string" />

              <xsd:attribute name="type" type="xsd:string" />

              <xsd:attribute name="mimetype" type="xsd:string" />

              <xsd:attribute ref="xml:space" />

            </xsd:complexType>

          </xsd:element>

          <xsd:element name="assembly">

            <xsd:complexType>

              <xsd:attribute name="alias" type="xsd:string" />

              <xsd:attribute name="name" type="xsd:string" />

            </xsd:complexType>

          </xsd:element>

          <xsd:element name="data">

            <xsd:complexType>

              <xsd:sequence>

                <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />

                <xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />

              </xsd:sequence>

              <xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />

              <xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />

              <xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />

              <xsd:attribute ref="xml:space" />

            </xsd:complexType>

          </xsd:element>

          <xsd:element name="resheader">

            <xsd:complexType>

              <xsd:sequence>

                <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />

              </xsd:sequence>

              <xsd:attribute name="name" type="xsd:string" use="required" />

            </xsd:complexType>

          </xsd:element>

        </xsd:choice>

      </xsd:complexType>

    </xsd:element>

  </xsd:schema>

  <resheader name="resmimetype">

    <value>text/microsoft-resx</value>

  </resheader>

  <resheader name="version">

    <value>2.0</value>

  </resheader>

  <resheader name="reader">

    <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>

  </resheader>

  <resheader name="writer">

    <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>

  </resheader>

  <data name="Name" xml:space="preserve">

    <value>Bejaoui Bechir</value>

  </data>

  <data name="Country" xml:space="preserve">

    <value>North Africa</value>

  </data>
</root>

You shouldn't be worry about all the content at first glance, as you see, all inserted parameters are located in the bottom of the page and nested within the

<data><value></value></datatags.

<data name="Name" xml:space="preserve">

    <value>Bejaoui Bechir</value>

  </data>

  <data name="Country" xml:space="preserve">

    <value>North Africa</value>
  </data>

The above elements are described in the table bellow:

Element Description
data The data tag is used to specify the resource attributes.

name(Necessary),
type(Optional but recommended), we should indicate the type it self like System.Int32, the name space such as System, the version, the culture and finally the public key token.
value It is the tag that value is wrapped in
xml: space It is used to identify the document if a white space is considered as important

This attribute accepts two values:

default: This option will treat the white space within a document as something neglected.

preserve:This option will treat the white space within a document as something that has meaning.

Concerning the part just above the first data tag, it's called the header or the resx file header. It provides a detailed description about resources. So, if I try to represent the above resx file, the figure should be as bellow:

The resx file


<?
xml version="1.0" encoding="utf-8"?>

<root>

Contains comments added by Microsoft to explain in adavantage the resx improuvements

<xsd:element 1>

Contains the resources descriptions according to the element1, namely, root in our case

</xsd:element 1>

<xsd:element 2>

Contains the resources descriptions according to the element2, namely, data in our case

<xsd:element 2> 

<resheader name="resmimetype"><value>The resource file type</value></resheader>

<resheader name="version"><value>2.0</value></resheader>

<resheader name="reader"><value>resx reader object</value></resheader>

<resheader name="writer"><value>resx writer object</value></resheader>

<data name="Name" xml:space="preserve">

    <value>Bejaoui Bechir</value>

</data>

<data name="Country" xml:space="preserve">

    <value>North Africa</value>

</data>

</root>

Untitled-2.gif

As a response to the second question, I can say, that a resx file can't be directly embedded in a runtime environment; it has to be converted to a resource file before. Therefore, they are two different resource files formats. And we will talk about how to generate a resource file form a resx file in subsequent articles. 

Resource file:

I can define this kind of file as a common language runtime binary file that one can embed within a runtime environment. In order to be used by the application core later. To understand better the approach, I suggest this kind of analogy.

In this case the code source can't used directly except that it will be compiled to (*.exe) or (*.dll) assembly, same think can be said according to the resx and resource file, the resx file represents the code source and the resource file represents the binary file either resource format file or an assembly satellite. We will see in next articles a walkthrough of how to generate assembly satellites from resx and text files and what for.

Up Next
    Ebook Download
    View all
    Learn
    View all