Redigeeri

Jagamisviis:


ResXDataNode Class

Definition

Represents an element in an XML resource (.resx) file.

public ref class ResXDataNode sealed : System::Runtime::Serialization::ISerializable
[System.Serializable]
public sealed class ResXDataNode : System.Runtime.Serialization.ISerializable
public sealed class ResXDataNode : System.Runtime.Serialization.ISerializable
[<System.Serializable>]
type ResXDataNode = class
    interface ISerializable
type ResXDataNode = class
    interface ISerializable
Public NotInheritable Class ResXDataNode
Implements ISerializable
Inheritance
ResXDataNode
Attributes
Implements

Examples

The following example uses the ResXResourceReader.GetEnumerator method to obtain an IDictionaryEnumerator object that is used to enumerate the ResXDataNode objects in a .resx file. The example includes a CreateResourceFile routine that creates the necessary XML resource file.

using System;
using System.Collections;
using System.ComponentModel.Design;
using System.Resources;

public class Example
{
   private const string resxFilename = @".\CountryHeaders.resx";
    
   public static void Main()
   {
      // Create a resource file to read.
      CreateResourceFile();
      
      // Enumerate the resources in the file.
      ResXResourceReader rr = new ResXResourceReader(resxFilename);
      rr.UseResXDataNodes = true;
      IDictionaryEnumerator dict = rr.GetEnumerator();
      while (dict.MoveNext()) {
         ResXDataNode node = (ResXDataNode) dict.Value;
         Console.WriteLine("{0,-20} {1,-20} {2}", 
                           node.Name + ":", 
                           node.GetValue((ITypeResolutionService) null), 
                           ! String.IsNullOrEmpty(node.Comment) ? "// " + node.Comment : "");
      }
   }

   private static void CreateResourceFile()
   {
      ResXResourceWriter rw = new ResXResourceWriter(resxFilename);
      string[] resNames = {"Country", "Population", "Area", 
                           "Capital", "LCity" };
      string[] columnHeaders = { "Country Name", "Population (2010}", 
                                 "Area", "Capital", "Largest City" };
      string[] comments = { "The localized country name", "Estimated population, 2010", 
                            "The area in square miles", "Capital city or chief administrative center", 
                            "The largest city based on 2010 data" };
      rw.AddResource("Title", "Country Information");
      rw.AddResource("nColumns", resNames.Length);
      for (int ctr = 0; ctr < resNames.Length; ctr++) {
         ResXDataNode node = new ResXDataNode(resNames[ctr], columnHeaders[ctr]);
         node.Comment = comments[ctr];
         rw.AddResource(node);
      }
      rw.Generate();
      rw.Close();
   }
}
// The example displays the following output:
//    Title:               Country Information
//    nColumns:            5
//    Country:             Country Name         // The localized country name
//    Population:          Population (2010}    // Estimated population, 2010
//    Area:                Area                 // The area in square miles
//    Capital:             Capital              // Capital city or chief administrative center
//    LCity:               Largest City         // The largest city based on 2010 data
Imports System.Collections
Imports System.ComponentModel.Design
Imports System.Resources

Module Example
   Private Const resxFilename As String = ".\CountryHeaders.resx"
     
   Public Sub Main()
      ' Create a resource file to read.
      CreateResourceFile()
      
      ' Enumerate the resources in the file.
      Dim rr As New ResXResourceReader(resxFilename)
      rr.UseResXDataNodes = True
      Dim dict As IDictionaryEnumerator = rr.GetEnumerator()
      Do While dict.MoveNext()
         Dim node As ResXDataNode = DirectCast(dict.Value, ResXDataNode)
         Console.WriteLine("{0,-20} {1,-20} {2}", 
                           node.Name + ":", 
                           node.GetValue(CType(Nothing, ITypeResolutionService)), 
                           If(Not String.IsNullOrEmpty(node.Comment), "// " + node.Comment, ""))
      Loop
   End Sub
   
   Private Sub CreateResourceFile()
      Dim rw As New ResxResourceWriter(resxFilename)
      Dim resNames() As String = {"Country", "Population", "Area", 
                                  "Capital", "LCity" }
      Dim columnHeaders() As String = { "Country Name", "Population (2010}", 
                                        "Area", "Capital", "Largest City" }
      Dim comments() As String = { "The localized country name", "Estimated population, 2010", 
                                   "The area in square miles", "Capital city or chief administrative center", 
                                   "The largest city based on 2010 data" }
      rw.AddResource("Title", "Country Information")
      rw.AddResource("nColumns", resNames.Length)
      For ctr As Integer = 0 To resNames.Length - 1
         Dim node As New ResXDataNode(resNames(ctr), columnHeaders(ctr))
         node.Comment = comments(ctr)
         rw.AddResource(node)
      Next
      rw.Generate()
      rw.Close()
   End Sub
End Module
' The example displays the following output:
'    Title:               Country Information
'    nColumns:            5
'    Country:             Country Name         // The localized country name
'    Population:          Population (2010}    // Estimated population, 2010
'    Area:                Area                 // The area in square miles
'    Capital:             Capital              // Capital city or chief administrative center
'    LCity:               Largest City         // The largest city based on 2010 data

Because the UseResXDataNodes property is true, the value of the IDictionaryEnumerator.Value property is a ResXDataNode object rather than the resource value. This makes a resource item's comment available from the ResXDataNode.Comment property.

Remarks

Important

Calling methods from this class with untrusted data is a security risk. Call the methods from this class only with trusted data. For more information, see Validate All Inputs.

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, so long as the object supports serialization and type editors.

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 calling the ResXResourceWriter.AddResource method.

To retrieve an existing ResXDataNode object, you must enumerate the ResXDataNode objects in an XML resource file by instantiating a ResXResourceReader object, setting the ResXResourceReader.UseResXDataNodes property to true, and calling the ResXResourceReader.GetEnumerator method to get an enumerator. The example provides an illustration.

Constructors

ResXDataNode(String, Object, Func<Type,String>)

Initializes a new instance of the ResXDataNode class.

ResXDataNode(String, Object)

Initializes a new instance of the ResXDataNode class.

ResXDataNode(String, ResXFileRef, Func<Type,String>)

Initializes a new instance of the ResXDataNode class with a reference to a resource file.

ResXDataNode(String, ResXFileRef)

Initializes a new instance of the ResXDataNode class with a reference to a resource file.

Properties

Comment

Gets or sets an arbitrary comment regarding this resource.

FileRef

Gets the file reference for this resource.

Name

Gets or sets the name of this resource.

Methods

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetNodePosition()

Retrieves the position of the resource in the resource file.

GetType()

Gets the Type of the current instance.

(Inherited from Object)
GetValue(AssemblyName[])

Retrieves the object that is stored by this node by searching the specified assemblies.

GetValue(ITypeResolutionService)

Retrieves the object that is stored by this node by using the specified type resolution service.

GetValueTypeName(AssemblyName[])

Retrieves the type name for the value by examining the specified assemblies.

GetValueTypeName(ITypeResolutionService)

Retrieves the type name for the value by using the specified type resolution service.

MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ToString()

Returns a string that represents the current object.

(Inherited from Object)

Explicit Interface Implementations

ISerializable.GetObjectData(SerializationInfo, StreamingContext)

Populates a SerializationInfo object with the data needed to serialize the target object.

Applies to

See also