Export (0) Print
Expand All

ResXResourceReader.UseResXDataNodes Property

Gets or sets a value that indicates whether ResXDataNode objects are returned when reading the current XML resource file or stream.

Namespace:  System.Resources
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public bool UseResXDataNodes { get; set; }

Property Value

Type: System.Boolean
true if resource data nodes are retrieved; false if resource data nodes are ignored.

ExceptionCondition
InvalidOperationException

In a set operation, the enumerator for the resource file or stream is already open.

You can set the UseResXDataNodes property before you begin enumerating resources. By default, its value is false.

The following example enumerates and displays resource items in an XML file. Initially, UseResXDataNodes is set to false to show both data and metadata items as they occur in the resource file. The second enumeration has the UseResXDataNodes set to true so that resource data elements are returned as ResXDataNode objects.

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

The resource XML file used by the example contains one data node and one metadata node. To use the XML file with the previous example, copy the following text and save it to a file named 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>

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft