XmlDictionary Class

Represents a dictionary of strings that can be represented by an integer value. This is useful for optimizing the serialization and deserialization of XML documents that contain many repeated strings.

Constructors
XmlDictionary()
()
XmlDictionary(int capacity)
(int capacity)
XmlDictionary(XmlDictionary other)
(XmlDictionary other)
Methods
Add(string value)
int Add(string value)
Add(XmlDictionaryString value)
void Add(XmlDictionaryString value)
Add(int key, string value)
void Add(int key, string value)
Contains(string value)
bool Contains(string value)
FindKey(string value)
int FindKey(string value)
GetString(int key)
string GetString(int key)
GetOrAdd(string value)
XmlDictionaryString GetOrAdd(string value)
Item(int key)
string Item(int key)
Item(string value)
XmlDictionaryString Item(string value)
Properties
Count
int Count { get; }
Capacity
int Capacity { get; set; }

Remarks

The XmlDictionary class is part of the .NET Framework's XML processing capabilities. It enables efficient handling of repeated string values within XML data by mapping them to unique integer identifiers. This is particularly beneficial when dealing with large XML documents or scenarios where performance is critical.

Usage Example


using System;
using System.Xml;

public class Example
{
    public static void Main(string[] args)
    {
        XmlDictionary dictionary = new XmlDictionary();

        // Add strings to the dictionary
        int key1 = dictionary.Add("Hello");
        int key2 = dictionary.Add("World");
        int key3 = dictionary.Add("Hello"); // This will return the same key as "Hello"

        Console.WriteLine($"Key for 'Hello': {key1}");
        Console.WriteLine($"Key for 'World': {key2}");
        Console.WriteLine($"Key for second 'Hello': {key3}"); // Should be the same as key1

        // Retrieve strings using their keys
        string str1 = dictionary.GetString(key1);
        string str2 = dictionary.GetString(key2);

        Console.WriteLine($"String for key {key1}: {str1}");
        Console.WriteLine($"String for key {key2}: {str2}");

        // Using GetOrAdd to get an XmlDictionaryString
        XmlDictionaryString dictStr = dictionary.GetOrAdd("MSDN Example");
        Console.WriteLine($"XmlDictionaryString value: {dictStr.Value}");
        Console.WriteLine($"XmlDictionaryString key: {dictStr.Key}");
    }
}
            

See Also