Condividi tramite


ResXResourceReader.UseResXDataNodes Proprietà

Definizione

Ottiene o imposta un valore che indica se gli oggetti ResXDataNode vengono restituiti durante la lettura del flusso o del file di risorse XML corrente.

public:
 property bool UseResXDataNodes { bool get(); void set(bool value); };
public bool UseResXDataNodes { get; set; }
member this.UseResXDataNodes : bool with get, set
Public Property UseResXDataNodes As Boolean

Valore della proprietà

true se vengono recuperati i nodi dati della risorsa; false se vengono ignorati.

Eccezioni

In un'operazione di impostazione, l'enumeratore del file o del flusso di risorse è già aperto.

Esempio

Nell'esempio seguente vengono enumerati e visualizzati gli elementi delle risorse in un file XML. Inizialmente, UseResXDataNodes è impostato su false per visualizzare sia gli elementi di dati che di metadati mentre si verificano nel file di risorse. La seconda enumerazione ha l'oggetto UseResXDataNodes impostato su true in modo che gli elementi dati della risorsa vengano restituiti come ResXDataNode oggetti .

#using <System.dll>
#using <System.Windows.Forms.dll>

using namespace System;
using namespace System::Collections;
using namespace System::Resources;
using namespace System::ComponentModel::Design;

namespace UseDataNodesExample
{
    public ref class Program
    {
    public:
        static void Main()
        {
            Console::WriteLine("\nEnumerating as data items...");
            EnumResourceItems("Resource1.resx", false);

            Console::WriteLine("\nEnumerating as data nodes...");
            EnumResourceItems("Resource1.resx", true);
        }

        static void EnumResourceItems(String^ resxFile, bool useDataNodes)
        {
            ResXResourceReader^ reader = gcnew ResXResourceReader(resxFile);

            reader->UseResXDataNodes = useDataNodes;

            // Enumerate using IEnumerable.GetEnumerator().
            Console::WriteLine("\n  Default enumerator:");
            for each (DictionaryEntry entry in reader)
            {
                ShowResourceItem(entry, useDataNodes);
            }

            // Enumerate using GetMetadataEnumerator()
            IDictionaryEnumerator^ metadataEnumerator = reader->GetMetadataEnumerator();

            Console::WriteLine("\n  MetadataEnumerator:");
            while (metadataEnumerator->MoveNext())
            {
                ShowResourceItem(metadataEnumerator->Entry, useDataNodes);
            }

            // Enumerate using GetEnumerator()
            IDictionaryEnumerator^ enumerator = reader->GetEnumerator();

            Console::WriteLine("\n  Enumerator:");
            while (enumerator->MoveNext())
            {
                ShowResourceItem(enumerator->Entry, useDataNodes);
            }
            delete reader;
        }

        static void ShowResourceItem(DictionaryEntry entry, bool isDataNode)
        {
            // Use a nullptr type resolver.
            ITypeResolutionService^ typeres = nullptr;
            ResXDataNode^ dnode;

            if (isDataNode)
            {
                // Display from node info.
                dnode = (ResXDataNode^)entry.Value;
                Console::WriteLine("  {0}={1}", dnode->Name, dnode->GetValue(typeres));
            }
            else
            {
                // Display as DictionaryEntry info.
                Console::WriteLine("  {0}={1}", entry.Key, entry.Value);
            }
        }
    };
}

int main()
{
    UseDataNodesExample::Program::Main();
}
// The example program will have the following output:
//
// Enumerating as data items...
//
//   Default enumerator:
//   DataSample=Sample DATA value
//
//   MetadataEnumerator:
//   MetadataSample=Sample METADATA value
//
//   Enumerator:
//   DataSample=Sample DATA value
//
// Enumerating as data nodes...
//
//   Default enumerator:
//   DataSample=Sample DATA value
//   MetadataSample=Sample METADATA value
//
//   MetadataEnumerator:
//
//   Enumerator:
//   DataSample=Sample DATA value
//   MetadataSample=Sample METADATA value
using System;
using System.Collections;
using System.Resources;
using System.ComponentModel.Design;

namespace UseDataNodesExample
{
    public class Program
    {
        public static void Main()
        {
            Console.WriteLine("\nEnumerating as data items...");
            EnumResourceItems("Resource1.resx", false);

            Console.WriteLine("\nEnumerating as data nodes...");
            EnumResourceItems("Resource1.resx", true);
        }

        public static void EnumResourceItems(string resxFile, bool useDataNodes)
        {
            using (ResXResourceReader reader = new ResXResourceReader(resxFile))
            {
                reader.UseResXDataNodes = useDataNodes;

                // Enumerate using IEnumerable.GetEnumerator().
                Console.WriteLine("\n  Default enumerator:");
                foreach (DictionaryEntry entry in reader)
                {
                    ShowResourceItem(entry, useDataNodes);
                }

                // Enumerate using GetMetadataEnumerator()
                IDictionaryEnumerator metadataEnumerator = reader.GetMetadataEnumerator();

                Console.WriteLine("\n  MetadataEnumerator:");
                while (metadataEnumerator.MoveNext())
                {
                    ShowResourceItem(metadataEnumerator.Entry, useDataNodes);
                }

                // Enumerate using GetEnumerator()
                IDictionaryEnumerator enumerator = reader.GetEnumerator();

                Console.WriteLine("\n  Enumerator:");
                while (enumerator.MoveNext())
                {
                    ShowResourceItem(enumerator.Entry, useDataNodes);
                }
            }
        }

        public static void ShowResourceItem(DictionaryEntry entry, bool isDataNode)
        {
            // Use a null type resolver.
            ITypeResolutionService typeres = null;
            ResXDataNode dnode;

            if (isDataNode)
            {
                // Display from node info.
                dnode = (ResXDataNode)entry.Value;
                Console.WriteLine("  {0}={1}", dnode.Name, dnode.GetValue(typeres));
            }
            else
            {
                // Display as DictionaryEntry info.
                Console.WriteLine("  {0}={1}", entry.Key, entry.Value);
            }
        }
    }
}

// The example program will have the following output:
//
// Enumerating as data items...
//
//   Default enumerator:
//   DataSample=Sample DATA value
//
//   MetadataEnumerator:
//   MetadataSample=Sample METADATA value
//
//   Enumerator:
//   DataSample=Sample DATA value
//
// Enumerating as data nodes...
//
//   Default enumerator:
//   DataSample=Sample DATA value
//   MetadataSample=Sample METADATA value
//
//   MetadataEnumerator:
//
//   Enumerator:
//   DataSample=Sample DATA value
//   MetadataSample=Sample METADATA value
Imports System.Collections
Imports System.Resources
Imports System.ComponentModel.Design

Namespace UseDataNodesExample
    Public Class Program
        Public Shared Sub Main()
            Console.WriteLine(Environment.NewLine + "Enumerating as data items...")
            EnumResourceItems("Resource1.resx", False)

            Console.WriteLine(Environment.NewLine + "Enumerating as data nodes...")
            EnumResourceItems("Resource1.resx", True)
        End Sub

        Public Shared Sub EnumResourceItems(resxFile As String, useDataNodes As Boolean)
            Using reader As New ResXResourceReader(resxFile)
                reader.UseResXDataNodes = useDataNodes

                ' Enumerate using IEnumerable.GetEnumerator().
                Console.WriteLine(Environment.NewLine + "  Default enumerator:")
                For Each entry As DictionaryEntry In reader
                    ShowResourceItem(entry, useDataNodes)
                Next entry

                ' Enumerate using GetMetadataEnumerator()
                Dim metadataEnumerator As IDictionaryEnumerator = reader.GetMetadataEnumerator()

                Console.WriteLine(Environment.NewLine + "  MetadataEnumerator:")
                While metadataEnumerator.MoveNext()
                    ShowResourceItem(metadataEnumerator.Entry, useDataNodes)
                End While

                ' Enumerate using GetEnumerator()
                Dim enumerator As IDictionaryEnumerator = reader.GetEnumerator()

                Console.WriteLine(Environment.NewLine + "  Enumerator:")
                While enumerator.MoveNext()
                    ShowResourceItem(enumerator.Entry, useDataNodes)
                End While
            End Using
        End Sub

        Public Shared Sub ShowResourceItem(entry As DictionaryEntry, isDataNode As Boolean)
            ' Use a Nothing type resolver.
            Dim typeres As ITypeResolutionService = Nothing
            Dim dnode As ResXDataNode

            If isDataNode Then
                ' Display from node info.
                dnode = CType(entry.Value, ResXDataNode)
                Console.WriteLine("  {0}={1}", dnode.Name, dnode.GetValue(typeres))
            Else
                ' Display as DictionaryEntry info.
                Console.WriteLine("  {0}={1}", entry.Key, entry.Value)
            End If
        End Sub
    End Class
End Namespace

' The example program will have the following output:
'
' Enumerating as data items...
'
'   Default enumerator:
'   DataSample=Sample DATA value
'
'   MetadataEnumerator:
'   MetadataSample=Sample METADATA value
'
'   Enumerator:
'   DataSample=Sample DATA value
'
' Enumerating as data nodes...
'
'   Default enumerator:
'   DataSample=Sample DATA value
'   MetadataSample=Sample METADATA value
'
'   MetadataEnumerator:
'
'   Enumerator:
'   DataSample=Sample DATA value
'   MetadataSample=Sample METADATA value

Il file XML di risorse usato dall'esempio contiene un nodo dati e un nodo di metadati. Per usare il file XML con l'esempio precedente, copiare il testo seguente e salvarlo in un file denominato Resource1.resx.

<?xml version="1.0" encoding="utf-8"?>  
<root>  
  <resheader name="resmimetype">  
    <value>text/microsoft-resx</value>  
  </resheader>  
  <resheader name="version">  
    <value>2.0</value>  
  </resheader>  
  <resheader name="reader">  
    <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0,  
           Culture=neutral, PublicKeyToken=b77a5c561934e089  
    </value>  
  </resheader>  
  <resheader name="writer">  
    <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0,  
           Culture=neutral, PublicKeyToken=b77a5c561934e089  
    </value>  
  </resheader>  
  <data name="DataSample" xml:space="preserve">  
    <value>Sample DATA value</value>  
  </data>  
  <metadata name="MetadataSample">  
    <value>Sample METADATA value</value>  
  </metadata>  
</root>  

Commenti

È possibile impostare la UseResXDataNodes proprietà prima di iniziare a enumerare le risorse. Per impostazione predefinita, il valore è false.

Si applica a