.NETGURU
XML Question
Messages   Related Types
This message was discovered on microsoft.public.dotnet.languages.csharp.

Post a new message to this list...

Michael C
Hi all,

I'm playing around with an XML structured like this:

<root>
<subone>
<setting name = "xyz" value = "100" />
</subone>
<subtwo>
<list name = "abcde" value = "1000" />
<list name = "fghij" value = "2000" />
</subtwo>
</root>

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? Any ideas are welcome.

Thanks in advance,
Michael C., MCDBA

Reply to this message...
 
    
Derek Harmon
"Michael C" <Click here to reveal e-mail address> wrote in message news:t%L0d.25202$Click here to reveal e-mail address...
[Original message clipped]

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.

- - - XmlAttributeCollector.cs
using System;
using System.Collections;
using System.Diagnostics;
using System.IO;
using System.Xml;

public class XmlAttributeCollector
{
private const int NOT_COLLECTING = -1;
private int currentArray;
private ArrayList[] subCollector;
private string[] atomizedTagNames;

// Creates an XmlAttributeCollector instance that will collect all attributes from the
// supplied tagnames (it also collects all attributes from ALL descendent elements
// of the supplied tagnames).
public XmlAttributeCollector( string[] tagNames)
{
this.currentArray = -1;
this.subCollector = new ArrayList[ tagNames.Length];
for( int i=0; i < this.subCollector.Length; ++i)
this.subCollector[ i] = new ArrayList( );
this.atomizedTagNames = 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 AddAttributeToCollector( XmlReader reader)
{
Debug.Assert( ( currentArray != NOT_COLLECTING ), "Bad context: not inside one of the atomized tag names.");

if ( reader.MoveToFirstAttribute( ) ) do
{
this.subCollector[ this.currentArray].Add( reader.Value);
}
while ( reader.MoveToNextAttribute( ) );
}

// Atomizes the tag names this XmlAttributeCollector is looking for -- atomization
// applies per-XmlReader instance.
private void AtomizeTagNames( XmlReader reader)
{
XmlNameTable nt = reader.NameTable;
for( int i=0; i < atomizedTagNames.Length; ++i)
atomizedTagNames[ i] = nt.Add( atomizedTagNames[ i]);
}

// Changes CurrentArray index to a nonnegative integer when the tagName argument
// matches one of the atomizedTagNames 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 UpdateCurrentArrayIndex( string tagName, XmlNodeType type)
{
int i = atomizedTagNames.Length;
while ( ( --i > NOT_COLLECTING ) && ( tagName != atomizedTagNames[ i] ) )
;
if ( i > NOT_COLLECTING )
this.currentArray = ( XmlNodeType.EndElement == type ) ? NOT_COLLECTING : i;
}

// Collects all XML attributes appearing in the tag names this XmlAttributeCollector
// 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 CollectXmlAttributes( XmlReader reader)
{
if ( null == reader )
throw new ArgumentNullException( "reader");

AtomizeTagNames( reader);
while ( reader.Read( ) )
{
UpdateCurrentArrayIndex( reader.LocalName, reader.NodeType);
if ( reader.NodeType == XmlNodeType.Element )
if ( this.currentArray != NOT_COLLECTING )
AddAttributeToCollector( 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 ArgumentNullException( "sink");
for ( int i = 0; i < this.atomizedTagNames.Length; ++i)
{
sink.WriteLine( "---- attribute values under <{0}> descendants ----", this.atomizedTagNames[ i]);
for ( int j = 0, jMax = this.subCollector[ i].Count; j < jMax; ++j)
sink.WriteLine( this.subCollector[ 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.atomizedTagNames.Length; ++i)
this.subCollector[ i].Clear( );
}
}
- - -

In your case, you might use this class with the following few lines of code:

XmlTextReader reader = new XmlTextReader( "root.xml");
XmlAttributeCollector collector = new XmlAttributeCollector( new string[] { "subone", "subtwo"} );
collector.CollectXmlAttributes( reader);
reader.Close( );
collector.DumpContents( 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 XmlAttributeCollector receives an XmlReader
(the call to CollectXmlAttributes( )) 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

Reply to this message...
 
    
Michael C
Thanks Derek,

I'm going to try your method.

Michael C., MCDBA

"Derek Harmon" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
> "Michael C" <Click here to reveal e-mail address> wrote in message
news:t%L0d.25202$Click here to reveal e-mail address...
> > 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
[Original message clipped]

Reply to this message...
 
 
System.ArgumentNullException
System.Collections.ArrayList
System.Console
System.Diagnostics.Debug
System.IO.TextWriter
System.Xml.NameTable
System.Xml.XmlDocument
System.Xml.XmlNameTable
System.Xml.XmlNodeType
System.Xml.XmlReader
System.Xml.XmlTextReader




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification