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 7867263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:39:53+00:00 2026-06-03T00:39:53+00:00

I have a custom property called Questions the code is below. public class Questions

  • 0

I have a custom property called Questions the code is below.

public class Questions
{
    private List<Question> _q = new List<Question>();

    public List<Question> Question
    {
        get { return _q; }
    } 
}

public class Question
{
    public string Text { get; set; }
    public string Name { get; set; }
    public string Type { get; set; }
    public string Answer { get; set; }
    private List<string> _Options = new List<string>(); 

    public List<string> Option {
      get { return _Options; }
    }

}

I then populate the property with the code below

    XmlDocument doc = new XmlDocument();
    Question q = new Question();
    Questions qs = new Questions();
    doc.Load(string.Format(@"questions.xml"));
    XmlNodeList list = doc.SelectNodes("/questions/question");
    foreach (XmlNode node in list)
    {

            q.Text = node.SelectSingleNode("text").InnerText;
            q.Type = node.SelectSingleNode("type").InnerText;
            q.Name = node.SelectSingleNode("name").InnerText;
            XmlNodeList options = doc.SelectNodes("/questions/question/options");
            foreach (XmlNode option in options)
            {
                q.Option.Add(option.SelectSingleNode("option").InnerText);
            }
            load.Visible = false;
            qa.Visible = true;

            qs.Question.Add(q);
            DisplayQuestion(qs);

    }

Now when I try to access it with the code below I do not get the output I expect. And thus this is where I need help. The Sample XML is at the bottom

                Label1.Text = q.Question[CurrentQ].Text;
                for (int i = 0; i < q.Question[CurrentQ].Option.Count; i++)
                {
                    CheckBoxList1.Items.Add(q.Question[CurrentQ].Option[i]);
                }

XML:

<?xml version="1.0"?>
<questions>
  <question>
    <num>1</num>
    <type>radio</type>
    <text>Do you like cake?</text>
    <options>
      <option>Yes</option>
      <option>No</option>
      <option>Sometimes</option>
    </options>
    <name>cake</name>
  </question>
  <question>
    <num>2</num>
    <type>dropdown</type>
    <text>Do you like TV?</text>
    <options>
      <option>Yes</option>
      <option>No</option>
      <option>Sometimes</option>
    </options>
    <name>tv</name>
  </question>
  <question>
    <num>3</num>
    <type>checkbox</type>
    <text>What do you like?</text>
    <options>
      <option>Cake</option>
      <option>TV</option>
      <option>Flipper Reruns</option>
    </options>
    <name>flipper</name>
  </question>
</questions>

And the output:

What do you like?

Yes

Yes

Cake

Yes

Yes

Cake

Yes

Yes

Cake

  • 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-06-03T00:39:54+00:00Added an answer on June 3, 2026 at 12:39 am

    The answer to this is fairly simple.
    the error is in the piece of code below.

    XmlDocument doc = new XmlDocument();
    Question q = new Question();
    Questions qs = new Questions();
    doc.Load(string.Format(@"questions.xml"));
    XmlNodeList list = doc.SelectNodes("/questions/question");
    foreach (XmlNode node in list)
    {
    
            q.Text = node.SelectSingleNode("text").InnerText;
            q.Type = node.SelectSingleNode("type").InnerText;
            q.Name = node.SelectSingleNode("name").InnerText;
            XmlNodeList options = doc.SelectNodes("/questions/question/options");
            foreach (XmlNode option in options)
            {
                q.Option.Add(option.SelectSingleNode("option").InnerText);
            }
            load.Visible = false;
            qa.Visible = true;
    
            qs.Question.Add(q);
            DisplayQuestion(qs);
    
    }
    

    C# automatically works by reference ( meaning pointer to an object instead of copying it).
    So in the above statement you declare Question q = new Question(); then you fill it and add it to your list and fill it again. this means only one Question is instantiated and therefor the result is all the same.

    XmlDocument doc = new XmlDocument();
    
    Questions qs = new Questions();
    doc.Load(string.Format(@"questions.xml"));
    XmlNodeList list = doc.SelectNodes("/questions/question");
    foreach (XmlNode node in list)
    {
            Question q = new Question(); // <--- Look here
    
            q.Text = node.SelectSingleNode("text").InnerText;
            q.Type = node.SelectSingleNode("type").InnerText;
            q.Name = node.SelectSingleNode("name").InnerText;
            XmlNodeList options = doc.SelectNodes("/questions/question/options");
            foreach (XmlNode option in options)
            {
                q.Option.Add(option.SelectSingleNode("option").InnerText);
            }
            load.Visible = false;
            qa.Visible = true;
    
            qs.Question.Add(q);
    
    
    }
    DisplayQuestion(qs); //<-- And here
    

    This way everytime a new question gets instantiated and added to the list instead of overwritten. Because in your questionList every entry is only a pointer to the one instance of the question object you have.

    Oh and why did i put the DisplayQuestion(qs); down there?
    Because you dont want to draw the entire list over and over again do we?

    make sure your Displayquestion function lops trough each question and draws the right controls

    Kind Regard Roxas
    I hope this helps

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

say, I have a partial class that contains a custom property like this public
I have a class with a custom indexer like so public string this[VehicleProperty property]
I have developed a custom control with two public property called ListField and DataSource.
I have a custom object Foo with a boolean property called Flagged and when
Before I start, I have this code inside of a Custom Usercontrol: private DependencyProperty
If I have a custom class called Tires: #import <Foundation/Foundation.h> @interface Tires : NSObject
I have a list of my custom class(which has properties like Name,Age,Address).How can i
I have created a user control which exposes a custom type property called SoftwareItem.
I have a model called Product with a custom property: def _get_active(self): o =
I have a view model public class ViewModel { public string Text { 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.