Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6008193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:47:24+00:00 2026-05-23T01:47:24+00:00

I am working on a page that uses XPATH to traverse an XML document

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T01:47:25+00:00Added an answer on May 23, 2026 at 1:47 am

    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") to SelectSingleNode("quizobjects") to get the XML element you want.

    Presentation.xml file:

    <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>
            </quizobject>
          </quizobjects>
        </Slide>
        <Slide id="slide id">
          <Filename>Name of file</Filename>
          <Title>Title of slide</Title>
        </Slide>
      </Slides>
    
    </Presentation>
    

    C# Code:

    XmlDocument doc     = new XmlDocument();
    
    try
    {
        using (var reader = XmlReader.Create("Presentation.xml"))
        {
            int numSlides;
    
            doc.Load(reader);
    
            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");
            XmlNodeList xnList = root.SelectNodes("/Presentation/Slides/Slide");
    
            //create array to hold location of quizzes
            int[] quizLocArray = new int[xnQuiz.Count];
    
            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)
                {
                    quizLocArray[j] = (i + 1);
                    j++;
                }
            }
        }
    }
    catch (Exception exception)
    {
        Console.WriteLine(exception.Message);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on something that uses Zen Cart. Changing the site from Zen
I'm working on set of webparts that uses common library. To testing deployment I
I've been working a Django-CMS plugin that uses the Flickr API, and after much
I'm working on a Rails 3 application that uses devise for user authentication. I
I am working on a module where i have a page that must have
I am creating a micro site that uses Javascript for data visualisation. I am
I have a PHP script that uses cURL to access a file also located
Not sure if the derived page is actually relevant to the problem here, but
I'm working with an ASP.net web application. I've written a user control called LocationSelector
I've upgraded a web application from .NET 3.5 to .NET 4, and I get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.