ResXDataNode 類別

定義

表示 XML 資源檔 (.resx) 中的項目。

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
繼承
ResXDataNode
屬性
實作

範例

下列範例會 ResXResourceReader.GetEnumerator 使用 方法來取得 IDictionaryEnumerator 物件,該對像是用來列舉 ResXDataNode .resx 檔案中的物件。 此範例包含建立 CreateResourceFile 必要 XML 資源檔的例程。

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

UseResXDataNodes因為 屬性是 true,所以 屬性的值IDictionaryEnumerator.ValueResXDataNode 物件,而不是資源值。 這會讓資源專案的批注可從 ResXDataNode.Comment 屬性取得。

備註

重要

使用不信任的資料呼叫此類別的方法,會造成安全性上的風險。 呼叫此類別的方法時,請一律使用信任的資料。 如需詳細資訊,請參閱 驗證所有輸入

類別 ResXDataNode 支援資源文件中豐富數據類型的表示。 只要物件支援串行化和類型編輯器,它就可以支援資源檔中任何物件的儲存。

您可以呼叫其中一個多載類別建構函式來建立 ResXDataNode 物件。 然後,您可以呼叫 ResXResourceWriter.AddResource 方法,將資源專案或專案加入資源檔。

若要擷取現有的ResXDataNode物件,您必須藉由具ResXResourceReader現化 物件、將 ResXResourceReader.UseResXDataNodes 屬性設定為 true,以及呼叫 ResXResourceReader.GetEnumerator 方法來取得列舉值,以列舉 ResXDataNode XML 資源檔中的物件。 這個範例將提供說明。

建構函式

ResXDataNode(String, Object)

初始化 ResXDataNode 類別的新執行個體。

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

初始化 ResXDataNode 類別的新執行個體。

ResXDataNode(String, ResXFileRef)

使用資源檔的參考,初始化 ResXDataNode 類別的新執行個體。

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

使用資源檔的參考,初始化 ResXDataNode 類別的新執行個體。

屬性

Comment

取得或設定有關這個資源的任意註解。

FileRef

取得這個資源的檔案參考。

Name

取得或設定這個資源的名稱。

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetNodePosition()

擷取資源在資源檔中的位置。

GetType()

取得目前執行個體的 Type

(繼承來源 Object)
GetValue(AssemblyName[])

藉由搜尋指定的組件,擷取這個節點所儲存的物件。

GetValue(ITypeResolutionService)

藉由使用型別解析服務,擷取這個節點所儲存的物件。

GetValueTypeName(AssemblyName[])

藉由檢查指定的組件,擷取值的型別名稱。

GetValueTypeName(ITypeResolutionService)

藉由使用指定的型別解析服務,擷取值的型別名稱。

MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

明確介面實作

ISerializable.GetObjectData(SerializationInfo, StreamingContext)

SerializationInfo 物件中填入序列化目標物件所需的資料。

適用於

另請參閱