Xml ChildNodes iterator sorting by node name?

A

Andy

Hi all,

I'm writing a program which transforms an SGML document to XML, which I
then pull elements out of.

At a certain point, I have the following Xml snippit:

<affliates>

<person>Bob</person>
<xaff refid="ABC" />

<person>Karen</person>
<xaff refid="ABC" />
<xaff refid="XYZ" />

<person>Trish</person>
<xaff refid="XYZ" />

<station id="ABC">ABC</station>
<station id="XYZ">XYZ</station>

</affliates>

So the order of the nodes is important; an xaff element refers to the
most recent person element.

I get process the Xml as follows:

foreach( XmlElement e in doc.SelectSingleNode( "affliates" ) ) {
if ( e.Name == "person" ) {
// Record name
}
else if ( e.Name == "xaff" ) {
// Record last author name and assocate with this affliate
}
else if ( e.Name == "station" ) {
// record station
}

}

But it looks like the nodes are being ordered alphabetically by their
name (all of the person nodes are selected first, than the station,
then the xaff). Is there any way around this behavior? How can i
process the nodes in the order which is specified by the orignal xml
document?

I realize that putting the xaff within the person element would make
things much easier, but I don't have any control over the schema, nor
can I get it changed.

Thanks
Andy
 
G

Guest

Hi Andy, you could do something like:

XmlNode ndCurrent;
XmlDocument xmlDoc = <LOADED XML DOCUMENT>

//point to the first child element in the xml
ndCurrent = xmlDoc.DocumentElement.FirstChild;

//loop through all the nodes
while(ndCurrent != null)
{
//do some processing on ndCurrent

//move to the next sibling
ndCurrent = ndCurrent.NextSibling;
}


Hope that helps
Mark.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top