473,396 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Xml ChildNodes iterator sorting by node name?

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

Nov 17 '05 #1
1 5420
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.
"Andy" wrote:
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

Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Skip Montanaro | last post by:
I'm getting somewhat painfully acquainted with xml.dom.minidom. What is the relationship between its documentElement attribute and its childNodes list? I thought XML documents consisted of a...
6
by: Ron Brennan | last post by:
Good evening. <span id=fileList> <input type=hidden name=file id=file_0 value=Name/> </span> .... document.getElementById(fileList).childNodes The childNodes is giving me two children: 1)...
1
by: ason | last post by:
Hi, I just tried to replace a single XmlNode with several ChildNodes. When doing this with the following code i found out that if you get the ChildNodes with 'ChildNodes' - property the nodes...
1
by: chris yoker via DotNetMonster.com | last post by:
hiya, I have a function that replaces the nodenames in an xmlFile. I use a while loop.The problem is, if the element has no childNodes, then it isn't replaced.I want ALL the elements to be...
1
by: craigkenisston | last post by:
Hi, I'm pretty newbie on XML so I have this basic question. I have a node which has 5 ChildNodes : <RelatedLink> <DataUrl type="canonical">clickhere2.com/</DataUrl>...
3
by: Q1tum | last post by:
Hi all, I have a problem with getting the amount of childs in a XML structure, the strucure is somewhat like the following: <?xml version="1.0" encoding="iso-8859-1"?> <cms> <num>21</num>...
5
by: Moses | last post by:
HI The Value for childNodes.length differs with mozilla and IE Is it problem with my coding..... I could not under stood............. The following is the details
3
by: djp | last post by:
Hi I have to sort arraylist. I tried to do this using this page as a reference: http://www.java2s.com/Code/CSharp/Collections-Data-Structure/UseIComparer.htm I did it exactly the same way but...
2
by: milecimm | last post by:
Hi, I'm trying to loop through the nodes of my xml file. The info I need to get is located quite deep in about the fifth node but I want to incorporate in the output information that is located in my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.