Note
This class is similar to System.Resources.ResXDataNode
in System.Windows.Forms.dll. See the Comparison with System.Resources.ResXDataNode section for the differences.
[SerializableAttribute]
public sealed class ResXDataNode : ISerializable,
ICloneable
<SerializableAttribute>
Public NotInheritable Class ResXDataNode
Implements ISerializable, ICloneable
[SerializableAttribute]
public ref class ResXDataNode sealed : ISerializable,
ICloneable
[<SealedAttribute>]
[<SerializableAttribute>]
type ResXDataNode =
class
interface ISerializable
interface ICloneable
end
The ResXDataNode class supports the representation of rich data types within a resource file. It can support the storage of any object in a resource file.
You can create a ResXDataNode object by calling one of its overloaded class constructors. You can then add the resource item or element to a resource file by one of the following options:
To retrieve a ResXDataNode object from a resource you have the following options:
using System;
using System.Collections;
using System.IO;
using KGySoft.Resources;
public class Example
{
private const string resx = @"<?xml version='1.0' encoding='utf-8'?>
<root>
<data name='string'>
<value>Test string</value>
<comment>Default data type is string.</comment>
</data>
<metadata name='meta string'>
<value>Meta String</value>
</metadata>
<data name='int' type='System.Int32'>
<value>42</value>
</data>
<assembly alias='CustomAlias' name='System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' />
<data name='color' type='System.Drawing.Color, CustomAlias'>
<value>Red</value>
<comment>When this entry is deserialized in an unsafe way, System.Drawing assembly will be loaded.</comment>
</data>
<data name='bytes' type='System.Byte[]'>
<value>VGVzdCBieXRlcw==</value>
</data>
<data name='dangerous' mimetype='application/x-microsoft.net.object.binary.base64'>
<value>YmluYXJ5</value>
<comment>BinaryFormatter will throw an exception for this invalid content.</comment>
</data>
</root>";
public static void Main()
{
// In SafeMode the enumerator values will be ResXDataNode instances instead of deserialized objects
var reader = new ResXResourceReader(new StringReader(resx)) { SafeMode = true };
Console.WriteLine("____Resources in .resx:____");
Dump(reader.GetEnumerator());
Console.WriteLine("____Metadata in .resx:____");
Dump(reader.GetMetadataEnumerator());
}
private static void Dump(IDictionaryEnumerator enumerator)
{
while (enumerator.MoveNext())
{
var node = (ResXDataNode)enumerator.Value;
Console.WriteLine($"Name: {node.Name}");
Console.WriteLine($" Type: {node.TypeName}");
Console.WriteLine($" Alias value: {node.AssemblyAliasValue}");
Console.WriteLine($" MIME type: {node.MimeType}");
Console.WriteLine($" Comment: {node.Comment}");
Console.WriteLine($" Raw value: {node.ValueData}");
try
{
var value = node.GetValueSafe();
Console.WriteLine($" Real value: {value} ({value.GetType()})");
}
catch (Exception e)
{
Console.WriteLine($" Safe deserialization of the node threw an exception: {e.Message}");
try
{
var value = node.GetValue();
Console.WriteLine($" Real value (unsafe): {value} ({value.GetType()})");
}
catch (Exception ex)
{
Console.WriteLine($" Unsafe deserialization of the node threw an exception: {ex.Message}");
}
}
Console.WriteLine();
}
}
}
// The example displays the following output:
// ____Resources in .resx:____
// Name: string
// Type:
// Alias value:
// MIME type:
// Comment: Default data type is string.
// Raw value: Test string
// Real value: Test string (System.String)
//
// Name: int
// Type: System.Int32
// Alias value:
// MIME type:
// Comment:
// Raw value: 42
// Real value: 42 (System.Int32)
//
// Name: color
// Type: System.Drawing.Color, CustomAlias
// Alias value: System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
// MIME type:
// Comment: When this entry is deserialized in an unsafe way, System.Drawing assembly will be loaded.
// Raw value: Red
// Safe deserialization of the node threw an exception: Type "System.Drawing.Color, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" in the data at line 18, position 4 cannot be resolved.
// You may try to specify the expected type or use the unsafe GetValue if the resource is from a trusted source.
// Real value (unsafe): Color[Red] (System.Drawing.Color)
//
// Name: bytes
// Type: System.Byte[]
// Alias value:
// MIME type:
// Comment:
// Raw value: VGVzdCBieXRlcw==
// Real value: System.Byte[] (System.Byte[])
//
// Name: dangerous
// Type:
// Alias value:
// MIME type: application/x-microsoft.net.object.binary.base64
// Comment: BinaryFormatter will throw an exception for this invalid content.
// Raw value: YmluYXJ5
// Safe deserialization of the node threw an exception: In safe mode it is not allowed to deserialize resource "dangerous" because it was serialized by BinaryFormatter. Line 27, position 4.
// Unsafe deserialization of the node threw an exception: End of Stream encountered before parsing was completed.
//
// ____Metadata in .resx:____
// Name: meta string
// Type:
// Alias value:
// MIME type:
// Comment:
// Raw value: Meta String
// Real value: Meta String (System.String)
If instantiated from a System.Resources.ResXDataNode or System.Resources.ResXFileRef instance, an internal conversion into KGySoft.Resources.ResXDataNode and KGySoft.Resources.ResXFileRef automatically occurs.
Unlike System.Resources.ResXDataNode, this ResXDataNode implementation really preserves the original information stored in the .resx file. No deserialization, assembly loading and type resolving occurs until a deserialization is explicitly requested by calling the GetValue or GetValueSafe methods.
Incompatibility with System.Resources.ResXDataNode:
New features and improvements compared to System.Resources.ResXDataNode:
ResXDataNode(String, Object) | Initializes a new instance of the ResXDataNode class. |
ResXDataNode(String, ResXFileRef, String) | Initializes a new instance of the ResXDataNode class with a reference to a resource file. |
AssemblyAliasValue | Gets the assembly name defined in the source .resx file if TypeName contains an assembly alias name, or , if TypeName contains the assembly qualified name. If the resource does not contain the .resx information (that is, if the ResXDataNode was created from an object or the raw .resx data was removed on a GetValue call), then this property returns . |
Comment | Gets or sets an arbitrary comment regarding this resource. |
FileRef | Gets the file reference for this resource, or , if this resource does not have a file reference. |
MimeType | Gets the MIME type as it is stored in the .resx file for this resource, or , if the mimetype attribute was not defined in the .resx file. If the resource does not contain the .resx information (that is, if the ResXDataNode was created from an object or the raw .resx data was removed on a GetValue call), then this property returns . |
Name | Gets the name of this resource. |
TypeName | Gets the type information as String as it is stored in the source .resx file. It can be either an assembly qualified name, or a type name with or without an assembly alias name. If AssemblyAliasValue is not , this property value contains an assembly alias name. The property returns , if the type attribute is not defined in the .resx file. If the resource does not contain the .resx information (that is, if the ResXDataNode was created from an object or the raw .resx data was removed on a GetValue call), then this property returns . |
ValueData | Gets the raw value data as String as it was stored in the source .resx file. If the resource does not contain the .resx information (that is, if the ResXDataNode was created from an object or the raw .resx data was removed on a GetValue call), then this property returns . |
Clone | Creates a new ResXDataNode that is a copy of the current instance. |
GetNodeColumnPosition | Retrieves the column position of the resource in the resource file. |
GetNodeLinePosition | Retrieves the line position of the resource in the resource file. |
GetValue | Retrieves the object that is stored by this node. |
GetValueSafe(Boolean) | Retrieves the object that is stored by this node, not allowing loading assemblies during the possible deserialization. If the resource is not a natively supported type, then you should use the other overloads. |
GetValueSafe(Type, Boolean) | Retrieves the object that is stored by this node, specifying the expected type of the result. |
GetValueSafe(ITypeResolutionService, String, Boolean) |
Retrieves the object that is stored by this node, not allowing loading assemblies during the possible deserialization.
See the Remarks section of the other GetValueSafe overloads for details. Obsolete. |
GetValueSafeT(Boolean) | Retrieves the object that is stored by this node, specifying the expected type of the result. |
ToString |
Returns a string that represents the current object.
(Overrides ObjectToString) |
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) |