Implements a single-threaded System.Xml.XmlNameTable.
See Also: NameTable Members
Several classes, such as System.Xml.XmlDocument and System.Xml.XmlReader, use the NameTable class internally to store attribute and element names. When an element or attribute name occurs multiple times in an XML document, it is stored only once in the NameTable.
The names are stored as common language runtime (CLR) object types. This enables you to do object comparisons on these strings rather than a more expensive string comparison. These string objects are referred to as atomized strings.
The following example demonstrates the difference between equal string values and equal string objects using the System.Xml.NameTable class.
C# Example
using System; using System.Text; using System.Xml; class Ntable { public static void Main() { NameTable nameTable = new NameTable(); string str1 = "sunny"; StringBuilder strBuilder = new StringBuilder(); string str2 = strBuilder.Append("sun").Append("ny").ToString(); Console.WriteLine( "{0} : {1}", str1, str2 ); Console.WriteLine( "{0} : {1}", str1 == str2, (Object)str1==(Object)str2 ); string str3 = nameTable.Add(str1); string str4 = nameTable.Add(str2); Console.WriteLine( "{0} : {1}", str3, str4 ); Console.WriteLine( "{0} : {1}", str3 == str4, (Object)str3==(Object)str4 ); } }
The output is
sunny : sunny
True : False
sunny : sunny
True : True