Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Photos | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » XML .NET » Introduction to the resources .resx and .resources files: Part I

Introduction to the resources .resx and .resources files: Part I

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.

Author Rank:
Total page views :  25105
Total downloads : 
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
Become a Sponsor

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.



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>



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.


Login to add your contents and source code to this article
 About the author
 
Bechir Bejaoui
The author holds a master degree in NTIC specialized  in software developement delivered by the high school of communication SUPCOM, he also holds a bachelor degree in finance delivered by  the  economic sciences and  management  university of Tunis "FSEGT". He's a freelance developer since 2006. Actually woking on the WPF, .Net framewok 3.5, silverlight and the other .Net new features, in addition, he is painter and sculptor.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Microsoft Visual Studio 2010 Professional
Microsoft Visual Studio 2010 Professional will launch on April 12, but you can beat the rush and secure your copy today by pre-ordering at the affordable estimated retail price of $549 (US). Pre-order now.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Developer-Ready ASP.NET 2.0 Web Hosting with 3 MONTHS FREE
Now supporting .NET 3.0 Framework with Windows Workflow Foundation, Windows Communication Foundation (WCF), Windows Presentation Foundation (WPF), windows CardSpace (WCS)! Providing more flexibility for Developers with Web Services Support and a User/Permission Manger. Also supporting MS SQL 2005/2000 with Real-Time Backups, FREE Automated Attach .MDF Tool, FREE SQL Restore and Shrink SQL DB Tools, and SQL
 
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
Become a Sponsor
 Comments

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2009.6.2
 © 2010  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.