Reading XML is repeating some nodes values that are fine in the XML

Hi there.

I’m using the code below to read the XML that is below too.

The thing is that when I reach the level and i try to read the <n_id> value everything is OK and I get “jhon” the first time, but when the foreach loop does the job the second time instead of getting “jimmy” I get “jhon” once again.

The thing is that if I just read the <j_id> the first time I get the correct value “001” and when the foreach loop does it again, I also get the correct value “002”.

I’m totally lost on what is happening here :stuck_out_tongue:

Some help please :slight_smile:

		XmlNodeList juegos_list = xmlDoc.GetElementsByTagName ("juego");
		
		foreach (XmlNode juego_info in juegos_list) {
			
			XmlNodeList juego_content = juego_info.ChildNodes;

			foreach (XmlNode juego_sets in juego_content) {

				if (juego_sets.Name == "j_id") {
					Debug.Log("GAME_ID "+juego_sets.InnerText);
				}			
				
				if (juego_sets.Name == "nivel") {
			
					XmlNodeList nivel_content = juego_content.Item (1).ChildNodes;

					foreach (XmlNode nivel_sets in nivel_content) {
						
						if (nivel_sets.Name == "n_id") {
							Debug.Log("LEVEL_ID "+nivel_sets.InnerText);
						}
						
						if (nivel_sets.Name == "n_opcion") {
						
							XmlNodeList opcion_content = nivel_content.Item (1).ChildNodes;
							
							foreach (XmlNode opcion_sets in opcion_content) {
							
								if (opcion_sets.Name == "o_id") {
									Debug.Log("OPTION_ID "+ opcion_sets.InnerText);
								}	
															
							}
						}						
					}
				}
			}
		}

Here is the XML:

<juegos>
	<juego>
		<j_id>001</j_id>
		<nivel>
			<n_id>jhon</n_id>
			<n_opcion>
				<o_id>001</o_id>
			</n_opcion>
			<n_opcion>
				<o_id>002</o_id>
			</n_opcion>	
			<n_opcion>
				<o_id>003</o_id>
			</n_opcion>							
		</nivel>	
		<nivel>
			<n_id>jimmy</n_id>
			<n_opcion>
				<o_id>001</o_id>
			</n_opcion>
			<n_opcion>
				<o_id>002</o_id>
			</n_opcion>
			<n_opcion>
				<o_id>003</o_id>
			</n_opcion>						
		</nivel>													
	</juego>
	
	<juego>
		<j_id>002</j_id>	
		<nivel>
			<n_id>earl</n_id>
			<n_opcion>
				<o_id>001</o_id>
			</n_opcion>
			<n_opcion>
				<o_id>002</o_id>
			</n_opcion>	
			<n_opcion>
				<o_id>003</o_id>
			</n_opcion>							
		</nivel>
		<nivel>
			<n_id>frank</n_id>
			<n_opcion>
				<o_id>001</o_id>
			</n_opcion>
			<n_opcion>
				<o_id>002</o_id>
			</n_opcion>
			<n_opcion>
				<o_id>003</o_id>
			</n_opcion>						
		</nivel>												
	</juego>			
</juegos>

Ok.

I’ve found the problem…

In this part:

                if (juego_sets.Name == "nivel") {
                    XmlNodeList nivel_content = juego_content.Item (1).ChildNodes;

Instead of using “juego_content.Item (1)” I have to use “juego_sets.ChildNodes”, then everything is perfectly read :stuck_out_tongue:

The code is like that:

                if (juego_sets.Name == "nivel") {
                    XmlNodeList nivel_content = juego_sets.ChildNodes;

Of course I change that in every place I use that type or reference.

Cheers :smile: