"Michael C" <mi******@nospa m.org> wrote in message news:t%******** **************@ news4.srv.hcvln y.cv.net...
I'm trying to figure out the most efficient way to read the attributes of
<subone> child nodes into one array and <subtwo> child nodes into another
array?
There are two factors that should be present in an efficient solution. First, use
XmlNameTable to get atomized strings for the "subone" and "subtwo" element
tag names, this will speed up comparisons by doing reference comparisons
instead of more expensive string comparisons. Second, use an XmlReader
to stream through the instance document.
The following class should support what you're trying to do, and it does so in
a very efficient manner.
- - - XmlAttributeCol lector.cs
using System;
using System.Collecti ons;
using System.Diagnost ics;
using System.IO;
using System.Xml;
public class XmlAttributeCol lector
{
private const int NOT_COLLECTING = -1;
private int currentArray;
private ArrayList[] subCollector;
private string[] atomizedTagName s;
// Creates an XmlAttributeCol lector instance that will collect all attributes from the
// supplied tagnames (it also collects all attributes from ALL descendent elements
// of the supplied tagnames).
public XmlAttributeCol lector( string[] tagNames)
{
this.currentArr ay = -1;
this.subCollect or = new ArrayList[ tagNames.Length];
for( int i=0; i < this.subCollect or.Length; ++i)
this.subCollect or[ i] = new ArrayList( );
this.atomizedTa gNames = tagNames;
}
// Adds all attribute values of the element node on which reader is currently positioned
// to the Collector array the Current Array index specifies.
private void AddAttributeToC ollector( XmlReader reader)
{
Debug.Assert( ( currentArray != NOT_COLLECTING ), "Bad context: not inside one of the atomized tag names.");
if ( reader.MoveToFi rstAttribute( ) ) do
{
this.subCollect or[ this.currentArr ay].Add( reader.Value);
}
while ( reader.MoveToNe xtAttribute( ) );
}
// Atomizes the tag names this XmlAttributeCol lector is looking for -- atomization
// applies per-XmlReader instance.
private void AtomizeTagNames ( XmlReader reader)
{
XmlNameTable nt = reader.NameTabl e;
for( int i=0; i < atomizedTagName s.Length; ++i)
atomizedTagName s[ i] = nt.Add( atomizedTagName s[ i]);
}
// Changes CurrentArray index to a nonnegative integer when the tagName argument
// matches one of the atomizedTagName s this Collector is collecting for; or changes it
// to NOT_COLLECTING when type suggests I am leaving the scope of a tagName
// I'm collecting for.
private void UpdateCurrentAr rayIndex( string tagName, XmlNodeType type)
{
int i = atomizedTagName s.Length;
while ( ( --i > NOT_COLLECTING ) && ( tagName != atomizedTagName s[ i] ) )
;
if ( i > NOT_COLLECTING )
this.currentArr ay = ( XmlNodeType.End Element == type ) ? NOT_COLLECTING : i;
}
// Collects all XML attributes appearing in the tag names this XmlAttributeCol lector
// was created to collect attribute values for, and any descendants, into one or more
// arrays. If run on more than one XmlReader, attribute values from tag names of
// interest can be accumulated from multiple XML documents.
public void CollectXmlAttri butes( XmlReader reader)
{
if ( null == reader )
throw new ArgumentNullExc eption( "reader");
AtomizeTagNames ( reader);
while ( reader.Read( ) )
{
UpdateCurrentAr rayIndex( reader.LocalNam e, reader.NodeType );
if ( reader.NodeType == XmlNodeType.Ele ment )
if ( this.currentArr ay != NOT_COLLECTING )
AddAttributeToC ollector( reader);
}
}
// Dumps the attribute values collected for the tag names of interest to
// any TextWriter.
public void DumpContents( TextWriter sink)
{
if ( null == sink )
throw new ArgumentNullExc eption( "sink");
for ( int i = 0; i < this.atomizedTa gNames.Length; ++i)
{
sink.WriteLine( "---- attribute values under <{0}> descendants ----", this.atomizedTa gNames[ i]);
for ( int j = 0, jMax = this.subCollect or[ i].Count; j < jMax; ++j)
sink.WriteLine( this.subCollect or[ i][ j].ToString( ) );
}
sink.WriteLine( "---- ------- ----");
}
// If any attribute values have been collected, then this method resets all of the
// collectors to their initial empty state.
public void ResetContents( )
{
for( int i = 0; i < this.atomizedTa gNames.Length; ++i)
this.subCollect or[ i].Clear( );
}
}
- - -
In your case, you might use this class with the following few lines of code:
XmlTextReader reader = new XmlTextReader( "root.xml") ;
XmlAttributeCol lector collector = new XmlAttributeCol lector( new string[] { "subone", "subtwo"} );
collector.Colle ctXmlAttributes ( reader);
reader.Close( );
collector.DumpC ontents( Console.Out);
You'd pass the element names you're interested in collecting attribute values
from (both on them, and all of their descendants) and these tag names would
be atomized as soon as the XmlAttributeCol lector receives an XmlReader
(the call to CollectXmlAttri butes( )) because it requires the XmlReader's
NameTable to do so.
One cautionary note on choosing between XmlReader and XmlDocument for
efficiency. Frequently developers need to do more than one thing (like the
accumulation of attribute values) with an XML document. XmlReader is the
most expeditious way to get through the document in a forward-only, firehose
sense. Simple tasks like accumulating the attribute values under a select
number of elements can be very easily done. On the other hand, if you
require multiple tasks to be performed, and then make multiple passes
through the document with an XmlReader, the pendulum often swings
towards using an XmlDocument instead. After overcoming the hurdle
of reading the entire document and parsing the document into a node tree
(it must complete these steps before you can do any processing), the
XmlDocument may become a better candidate for consideration.
Derek Harmon