Cook Computing

XmlTextReader and NameTable

March 5, 2006 Written by Charles Cook

I've been using the .NET XmlTextReader class recently so Scott Hanselman's post XmlTextReader more and more caught my attention. I noticed his use of the NameTable property. I've never used this, writing coding like this


while (reader.Read()) 
{
  if (reader.NodeType==XmlNodeType.Element && reader.Name=="foo") 
  {
      ...
  }
}
instead of:

object foo = reader.NameTable.Add("foo");
while (reader.Read()) 
{
  if (reader.NodeType==XmlNodeType.Element 
    && Object.ReferenceEquals(reader.Name, foo)) 
  {
      ...
  }
}

Accord to MSDN:

Several classes, such as XmlDocument and 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.

I wrote some test code to check how inefficient my code has been and it turns out that use of NameTable does make a difference, for example in the order of 10% faster when testing with a 90 line XML document containing 60 matching elements. Given that this is in a test which reads the document and does nothing else, the use of NameTable is probably not going to make a significant difference to a more typical program, but because it involves very little extra code it is perhaps an optimization worth making prematurely.