XmlNameTable Class

Namespace: System.Xml
Assembly: System.Xml.dll

Represents a table for the names used in an XML document. This class provides a mechanism for efficient name lookup and comparison.

Overview

The XmlNameTable class is fundamental to how XML parsers handle names (element names, attribute names, namespace URIs, and prefixes) efficiently. Instead of comparing strings repeatedly, which can be time-consuming, XmlNameTable allows the parser to get a unique object representing each name. Subsequent comparisons of names are then done by comparing these object references, which is significantly faster than string comparison.

When an XML document is parsed, the XmlReader reads the names and can use an XmlNameTable to store and retrieve them. The XmlReader.NameTable property provides access to the XmlNameTable used by the reader.

Key Methods and Properties

Implementations

The .NET Framework provides a default implementation, DefaultXmlNameTable, which is often used internally by XmlReader implementations. You can also create your own custom implementations if you have specific performance requirements or scenarios.

Example Usage

Here's a simple example demonstrating how to use XmlNameTable:

using System;
using System.Xml;

public class XmlNameTableExample
{
    public static void Main(string[] args)
    {
        // Create a default XmlNameTable
        XmlNameTable nameTable = new System.Xml.XmlNameTable();

        // Add some names
        string name1 = nameTable.Add("ElementName");
        string name2 = nameTable.Add("http://www.example.com");
        string name3 = nameTable.Add("attr");

        Console.WriteLine($"Added 'ElementName': {name1 == "ElementName"}"); // Will output true

        // Retrieve a name
        string retrievedName = nameTable.Get("ElementName");
        Console.WriteLine($"Retrieved 'ElementName': {retrievedName}"); // Will output ElementName

        // Add a name that already exists
        string name1Again = nameTable.Add("ElementName");
        Console.WriteLine($"Is 'name1' the same object as 'name1Again'? {object.ReferenceEquals(name1, name1Again)}"); // Will output True

        // Check for a name not in the table
        string unknownName = nameTable.Get("NonExistent");
        Console.WriteLine($"Retrieved 'NonExistent': {(unknownName == null ? "null" : unknownName)}"); // Will output null

        // Example of efficient comparison
        string prefix = "";
        string localName = "Root";
        string namespaceURI = "http://schemas.microsoft.com/vstudio";

        Console.WriteLine($"Adding '{localName}' and '{namespaceURI}'...");
        string qualifiedName = nameTable.Add(localName);
        string qualifiedNamespace = nameTable.Add(namespaceURI);

        // Imagine this is from an XmlReader's current node
        bool isEqual = nameTable.Equals(prefix, localName, namespaceURI);
        Console.WriteLine($"Are '{prefix}', '{localName}', '{namespaceURI}' equal to an entry in the table? {isEqual}"); // Will output true if added earlier
    }
}

See Also

System.Xml XmlNameTable .NET API Reference Documentation XML Parsing