ResXResourceWriter Class

Writes resources in an XML resource (.resx) file or an output stream.

Definition

Namespace: KGySoft.Resources
Assembly: KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 9.0.0-preview.1
C#
public sealed class ResXResourceWriter : IResourceWriter, 
	IDisposable
Inheritance
Object    ResXResourceWriter
Implements
IDisposable, IResourceWriter

Remarks

Resources are specified as name/value pairs using the AddResource method.

If CompatibleFormat property is , ResXResourceWriter emits .resx files, which can be then read not just by ResXResourceReader but by the original System.Resources.ResXResourceReader class, too.

Example

The following example shows how to create a resource file by ResXResourceWriter and add different kind of resource objects to it. At the end it displays the resulting .resx file content.
C#
using System;
using System.Drawing;
using System.IO;
using KGySoft.Resources;

public class Example
{
    [Serializable]
    private class MyCustomClass
    {
        public string StringProp { get; set; }
        public int IntProp { get; set; }
    }

    public static void Main()
    {
        // Check the result with CompatibleFormat = true as well.
        // You will get a much longer result, which will be able to read by System.Resources.ResXResourceReader, too.
        var result = new StringWriter();
        using (var writer = new ResXResourceWriter(result) { CompatibleFormat = false })
        {
            writer.AddResource("string", "string value");
            writer.AddResource("int", 42);
            writer.AddResource("null", (object)null);
            writer.AddResource("file", new ResXFileRef(@"images\Image.jpg", typeof(Bitmap)));
            writer.AddResource("custom", new MyCustomClass { IntProp = 42, StringProp = "blah" });
        }

        Console.WriteLine(result.GetStringBuilder());
    }
}

// The example displays the following output:
// <?xml version="1.0" encoding="utf-8"?>
// <root>
//   <data name="string">
//     <value>string value</value>
//   </data>
//   <data name="int" type="System.Int32">
//     <value>42</value>
//   </data>
//   <assembly alias="KGySoft.CoreLibraries" name="KGySoft.CoreLibraries, Version=3.6.3.1, Culture=neutral, PublicKeyToken=b45eba277439ddfe" />
//   <data name="null" type="KGySoft.Resources.ResXNullRef, KGySoft.CoreLibraries">
//     <value />
//   </data>
//   <data name="file" type="KGySoft.Resources.ResXFileRef, KGySoft.CoreLibraries">
//     <value>images\Image.jpg;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
//   </data>
//   <data name="custom" mimetype="text/kgysoft.net/object.binary.base64">
//     <value>
//       PgAChAQFQkNvbnNvbGVBcHAxLCBWZXJzaW9uPTEuMC4wLjAsIEN1bHR1cmU9bmV1dHJhbCwgUHVibGljS2V5VG9rZW49bnVsbBVF
//       eGFtcGxlK015Q3VzdG9tQ2xhc3MBAhs8U3RyaW5nUHJvcD5rX19CYWNraW5nRmllbGQDEgAEYmxhaBg8SW50UHJvcD5rX19CYWNr
//       aW5nRmllbGQECEAqAA==
//     </value>
//   </data>
// </root>

Comparison with System.Resources.ResXResourceWriter 

Incompatibility with System.Resources.ResXResourceWriter:

  • The System version has several public string fields, which are not intended to be accessed by a consumer code. Therefore the following fields are missing (they are not public) in this version:
    • BinSerializedObjectMimeType
    • ByteArraySerializedObjectMimeType
    • DefaultSerializedObjectMimeType
    • ResMimeType
    • ResourceSchema
    • SoapSerializedObjectMimeType
    • Version
  • In this version there are no one parameter constructors. Instead, the second parameter (typeNameConverter) is optional. If used purely from C# (no reflection or whatsoever), this change is a compatible one.
  • This ResXResourceWriter is a sealed class.
  • After disposing the ResXResourceWriter instance or calling the Close method, calling the Generate method will throw an ObjectDisposedException.
  • The System.Resources.ResXResourceWriter.AddAlias method just populates an inner alias list causing that the alias will be recognized on further processing but will never be dumped into the output stream. In this ResXResourceWriter implementation the AddAlias method is somewhat different: not just registers the alias as a known one but also dumps that into the output stream. It can be specified though, whether the dump should be deferred until the alias is actually referenced for the first time.

New features and improvements compared to System.Resources.ResXResourceWriter:

Constructors

ResXResourceWriter(Stream, FuncType, String) Initializes a new instance of the ResXResourceWriter class that writes the resources to a specified stream.
ResXResourceWriter(String, FuncType, String) Initializes a new instance of the ResXResourceWriter class that writes the resources to a specified file.
ResXResourceWriter(TextWriter, FuncType, String) Initializes a new instance of the ResXResourceWriter class that writes the resources by a specified textWriter.

Properties

AutoGenerateAlias Gets or sets whether an alias should be auto-generated for referenced assemblies.
Default value: .
BasePath Gets or sets the base path for the relative file path specified in a ResXFileRef object.
Default value: .
CompatibleFormat Gets or sets whether the ResXResourceWriter instance should create a System compatible .resx file, which can be used by the built-in resource editor of Visual Studio.
Default value: .
OmitHeader Gets or sets whether the header should be omitted. If both CompatibleFormat and OmitHeader are , then only the XML comment will be omitted. If CompatibleFormat is and OmitHeader is , then the comment, the .resx schema and the <resheader> elements will be omitted, too.
Default value: .
SafeMode Gets or sets whether it is prohibited to load assemblies when writing ResXDataNode instances whose raw .resx content is needed to be regenerated and whose value has not been deserialized yet. This can occur only if CompatibleFormat is , and when the ResXDataNode instances to write had been read from another .resx source previously.
Default value: .

Methods

AddAlias(String, AssemblyName, Boolean) Adds the specified alias to the mapping of aliases.
AddAlias(String, String, Boolean) Adds the specified alias to the mapping of aliases.
AddMetadata(ResXDataNode) Adds a metadata node specified in a ResXDataNode object to the list of resources to write.
AddMetadata(String, Byte) Adds a metadata node whose value is specified as a byte array to the list of resources to write.
AddMetadata(String, Object) Adds a metadata node whose value is specified as an object to the list of resources to write.
AddMetadata(String, String) Adds a metadata node whose value is specified as a string to the list of resources to write.
AddResource(ResXDataNode) Adds a named resource specified in a ResXDataNode object to the list of resources to write.
AddResource(String, Byte) Adds a named resource specified as a byte array to the list of resources to write.
AddResource(String, Object) Adds a named resource specified as an object to the list of resources to write.
AddResource(String, String) Adds a string resource to the resources.
Close Releases all resources used by the ResXResourceWriter. If content has not been saved yet (see Generate method), then firstly flushes any remaining content.
Dispose Releases all resources used by the ResXResourceWriter.
Finalize This member overrides the Finalize method.
(Overrides ObjectFinalize)
Generate Flushes all pending content into the output file, TextWriter or Stream.

Extension Methods

Convert Converts an Object specified in the obj parameter to the desired targetType.
See the Examples section of the generic ConvertTTarget(Object, CultureInfo) overload for an example.
(Defined by ObjectExtensions)
ConvertTTarget Converts an Object specified in the obj parameter to the desired TTarget.
(Defined by ObjectExtensions)
In Gets whether item is among the elements of set.
See the Examples section of the generic InT(T, T) overload for an example.
(Defined by ObjectExtensions)
TryConvert Tries to convert an Object specified in the obj parameter to the desired targetType.
See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example.
(Defined by ObjectExtensions)
TryConvert Tries to convert an Object specified in the obj parameter to the desired targetType.
See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example.
(Defined by ObjectExtensions)
TryConvertTTarget Tries to convert an Object specified in the obj parameter to the desired TTarget.
See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example.
(Defined by ObjectExtensions)
TryConvertTTarget Tries to convert an Object specified in the obj parameter to the desired TTarget.
See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example.
(Defined by ObjectExtensions)

See Also