Note
This class is similar to System.Resources.ResXResourceWriter
in System.Windows.Forms.dll. See the Comparison with System.Resources.ResXResourceWriter section for the differences.
public sealed class ResXResourceWriter : IResourceWriter,
IDisposable
Public NotInheritable Class ResXResourceWriter
Implements IResourceWriter, IDisposable
public ref class ResXResourceWriter sealed : IResourceWriter,
IDisposable
[<SealedAttribute>]
type ResXResourceWriter =
class
interface IResourceWriter
interface IDisposable
end
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.
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>
Incompatibility with System.Resources.ResXResourceWriter:
New features and improvements compared to System.Resources.ResXResourceWriter:
Type | Improvement | How it is handled by the System version |
---|---|---|
value | Invalid .resx representations of value is fixed when re-written by ResXResourceWriter. | When the System version rewrites such an invalid node, it turns into empty string. |
Array of SByte | Serializing sbyte[] type works properly. | The System.Resources.ResXResourceWriter changes the sbyte[] types to byte[]. |
Char | Support of unpaired surrogate characters. | System.Resources.ResXResourceWriter cannot serialize unpaired surrogates, though System.Resources.ResXResourceReader can read them successfully if they are serialized by ResXResourceWriter and CompatibleFormat is . |
String and any type serialized by a TypeConverter. | Strings containing unpaired surrogates and invalid Unicode characters can be written without any error. | System.Resources.ResXResourceWriter cannot serialize such strings, though System.Resources.ResXResourceReader can read them successfully if they are serialized by ResXResourceWriter and CompatibleFormat is . |
DateTime and DateTimeOffset | Serialized in a different way so even the milliseconds part is preserved. | The fixed form can be deserialized by System.Resources.ResXResourceReader, too; however, the DateTime.Kind will be always Local. |
Single, Double and Decimal | -0 (negative zero) value is handled correctly. | The fixed form can be deserialized by System.Resources.ResXResourceReader, too; however, in case of Single and Double -0 will always turn to +0. |
IntPtr, UIntPtr, DBNull and Type instances containing a runtime type. | These types are supported natively (without a mimetype attribute). Only if CompatibleFormat is . | System.Resources.ResXResourceWriter can serialize these type only by BinaryFormatter. Though in .NET Core and .NET Standard Type is not serializable even by BinaryFormatter. |
Generic types | Generic types with a TypeConverter are handled correctly. | Parsing generic type names may fail with System.Resources.ResXResourceReader. The problem does not occur on binary serialization because in that case the type name is not dumped into the .resx file but is encoded in the binary stream. |
Any non-serializable type | As long as the BinarySerializationFormatter can serialize the non-serializable type, this implementation supports non-serializable types as well. This works even if CompatibleFormat is . | If CompatibleFormat is during serialization, deserialization works even with System.Resources.ResXResourceReader as long as KGySoft.CoreLibraries assembly can be loaded and BinaryFormatter can find the AnyObjectSerializerWrapper class. |
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. |
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: . |
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. |
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) |