I am working on a page that uses XPATH to traverse an XML document to pull certain data elements and build a string based on them. I am able to count the elements correctly, but when trying to order traverse through them some of the elements that are being counted are not showing up. Most likely the task could be accomplished more efficiently, any assistance on accomplishing this task correctly would be appreciated.
XML
<?xml version="1.0" encoding="UTF-16"?>
<Presentation>
<Filename>Name of file</Filename>
<version>1.2</version>
<threshold>23</threshold> <!-- gives number of slides -->
<Slides>
<Slide id="slide id">
<Filename>Name of file</Filename>
<Title>Title of slide</Title>
</Slide>
<Slide id="slide id">
<Filename>Name of file</Filename>
<Title>Title of slide</Title>
<quizobjects>
<quizobject id="1">
<filename>Name of quiz</filename>
</quizobjects>
</Slide>
<Slide id="slide id">
<Filename>Name of file</Filename>
<Title>Title of slide</Title>
</Slide>
...etc
</Slides>
</Presentation>
Here is an example of the XML. I traverse through the slides counting them, as well as counting the quizobjects. (This returns correct numbers) However when I traverse through all slides trying to get the location of each quiz in the Slides node, it never hits any quizobjects.
C#
int numSlides;
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlToParse.Text); //pass in xml string
XmlElement root = doc.DocumentElement;
//get number of slides from threshold node
numSlides = Convert.ToInt32(root.SelectSingleNode("//threshold").InnerText);
//get number of quizzes/slides
XmlNodeList xnQuiz = root.SelectNodes("/Presentation/Slides/Slide/quizobjects"); //returns 7
XmlNodeList xnList = root.SelectNodes("/Presentation/Slides/Slide");
int[] quizLocArray = new int[xnQuiz.Count]; //create array to hold location of quizzes
int j = 0;
//find index of quizzes in slide list
for(int i = 0; i < xnList.Count; i++)
{
XmlNode quiz = xnList[i].SelectSingleNode("/quizobjects");
if(quiz != null) //stepping through quiz always equals null
{
quizLocArray[j] = (i + 1);
j++;
}
}
Output
numSlides/Total Number of Slides in XML: 23
xnQuiz.Count/Total Number of Quizzes in XML: 7
String.Join(",", quizLocArray)/Array of indexes of quizzes in slide list: 0,0,0,0,0,0,0
It appears as if your XML data is not properly formatted, as you are missing a closing
</quizobject>. I am going to go ahead and assume this is just a typo.You need to change your XPath query from
SelectSingleNode("/quizobjects")toSelectSingleNode("quizobjects")to get the XML element you want.Presentation.xml file:
C# Code: