Nested SelectNodes

Hello, I’m trying to iterate through an XML file. I have nested iterations that are returning more data (all of it, actually) than I intend.

The xml is here:
http://www.oldno19.com/upload/timeline.xml

Here is my code:

function loadXML() {
var www = new WWW ("http://www.oldno19.com/upload/timeline.xml");
yield www;
var xml = new XmlDocument();
xml.LoadXml(www.text);

var i : int = 1;
for (var node in xml.SelectNodes ("/how_we_got_here_stories/year/title")) {
	var thisCube = Instantiate(cube, Vector3 (i*1.25, 1, 0), Quaternion.identity) as Transform;
	yearTxt = thisCube.Find("YearText").GetComponent(TextMesh);
	titleTxt = thisCube.Find("TitleText").GetComponent(TextMesh);
	yearTxt.text = node.InnerText;
	thisCube.transform.parent = this.transform;
	i++;
	
    for (var node2 in node.SelectNodes ("/how_we_got_here_stories/year/how_we_got_here_story/title")) {
    	var thisCube2 = Instantiate(cube, Vector3 (i*1.25, 1, 0), Quaternion.identity) as Transform;
    	yearTxt = thisCube2.Find("YearText").GetComponent(TextMesh);
		titleTxt = thisCube2.Find("TitleText").GetComponent(TextMesh); 
		yearTxt.text = "";
		titleTxt.text = WrapTxt(node2.InnerText,charLimit);
    	thisCube2.transform.parent = this.transform;
    	i++;
    }
}

}

In the 2nd for-loop, I want it to iterate through the first <“year”>'s children and return the <“how_we_got_here_story”> items that appear under the first year… then do the same for the next <“year”>… but for each year, i end up with EVERY <how_we_got_here_story> item.

I have been searching the msdn pages for xmldocument methods, but I’m uncertain how to use them since they don’t give javascript examples. :confused: Guess I should learn C# soon.

Thanks in advance!

Well I figured something out… less lines of code, actually. Not sure how I managed to finally figure it out, I just kept trying stuff until I got clues and then found some other problems to fix, etc… LUCKY!

Here’s the code in case anyone else can use it:

function loadXML() {
var www = new WWW ("http://www.oldno19.com/upload/timeline.xml");
yield www;
var xml = new XmlDocument();
xml.LoadXml(www.text);
//print(xml.InnerXml);

var i : int = 0;
var totalCount : int = 0;
var newYear : boolean = false;
for (var node in xml.SelectNodes ("/how_we_got_here_stories/year/title")) {
	newYear = true;
    for (var node2 in node.SelectNodes("../how_we_got_here_story/title")) {
    	var thisCube2 = Instantiate(cube, Vector3 (totalCount*1.25, 1, 0), Quaternion.identity) as Transform;
    	yearTxt = thisCube2.Find("YearText").GetComponent(TextMesh);
		titleTxt = thisCube2.Find("TitleText").GetComponent(TextMesh);  	
		if (newYear) {
			yearTxt.text = node.InnerText;
		}else{
			yearTxt.text = "";
		}
		newYear = false;
		titleTxt.text = WrapTxt(node2.InnerText,charLimit);
    	thisCube2.transform.parent = this.transform;
    	print("--"+node2.InnerText);
    	totalCount++;
    	//yield WaitForSeconds (1);
    }
    i++;
}

}