Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi;

I want to develop an Android application, in this application i want to get data in my xml file randomly, and show this question to user. Than user answer this question(right or wrong), application get another random question on screen and this will continue like this.

So, my question is how to get this data randomly in XML file.

I tried some methods to do that but this methods could not perform exactly as i want.

Here this methods;

C#
List<int> t = new List<int>();
          for (int i = 0; i < 4; i++)
          {
              t.Add(i);
          }
          int s = t[1];
          XDocument doc = XDocument.Load(@"C:\\Users\Onur\Desktop\deneme.xml");

          var question = doc.Descendants("s");

          foreach (var questions in question)
          {
              label1.Text = questions.Value;
          }

C#
XmlTextReader reader = new XmlTextReader(@"C:\\Users\Onur\Desktop\deneme.xml");
           reader.Read();

           while (reader.Read())
           {
               reader.MoveToElement();

               label1.Text = reader.Name;
           }

C#
XmlNode categorynode = null;
          XmlTextReader xmlrd = new XmlTextReader(@"C:\\Users\Onur\Desktop\deneme.xml");

          XmlDocument doc = new XmlDocument();
          doc.Load(xmlrd);

          foreach (XmlNode node in doc.ChildNodes)
          {

              if (node.Name == "questions")
              {
                  categorynode = null;
                  label1.Text = node.InnerText;
              }
          }

C#
XmlDocument doc = new XmlDocument();
            doc.Load (@"C:\\Users\Onur\Desktop\deneme.xml");


            XmlNodeList list = doc.GetElementsByTagName("questions");

            for (int i = 0; i < list.Count; i++)
            {
                label1.Text = list[i].InnerXml;
            }


C#
List<questions> q= new List<questions>();

           XmlTextReader xmlrd = new XmlTextReader(@"C:\\Users\Onur\Desktop\deneme.xml");

           XmlDocument doc = new XmlDocument();
           doc.Load(xmlrd);

           XmlNode root = doc.SelectSingleNode("//inventory");
           XmlNodeList nodelist = root.SelectNodes("questions");

           foreach (XmlNode n in nodelist)
           {
               questions q = new questions();
           }


And here is my XML trial file;

XML
<?xml version="1.0"?>
<inventory>
  <questions>
       <qone>3/2 + 1/2 = ?
            <qonechoises>
            A = 1
            B = 3
            C = 2
            D = 5
            </qonechoises>
            <qoneans>C</qoneans>
       </qone>
  </questions>
</inventory>



Thank you for your answers...
Posted

What is this loop supposed to achieve? It looks like it's just overwriting the data loaded on the previous iteration.
C#
foreach (XmlNode n in nodelist) {
  questions q = new questions();
}


I would consider changing the structure of your question file. You are embedding sequence information in element names and that's going to make your xpath navigation more difficult than it needs to be. The identity should be either an attribute of the element <myTag ID="1"... /> or a separate child element - whatever suits your problem best. For example a question element might be represented as below.

XML
<question>
  <number>1</number>

  <options>
    <option> 
      <number>1</number>
      <optionText>option 1 text</optionText>
    <option>
    <!-- As many options/possible answers as required -->
    <option> 
      <optionNumber>n</optionNumber>
      <optionText>option  n text</optionText>
    <option>
  </options>

  <!-- Allow for questions with more than one correct response -->
  <responses>
   <response>1</response>
  </responses>

</question>


If you then have a question class, with supporting classes option and response, something like :

C#
class question {
  public question(XmlNode q) {/*load question from XML node*/}
  public Number {get; set;}

  // List of options to choose from.
  public List<option> Options {get; set;}

  // List of valid responses
  public List<response> Responses {get; set;}

  // User answers
  public List<answer> Answers {get; set;}
}


You can then initialise your list of questions something like this:
C#
List<question> unanswered = new List<question>();
List<question> answered  = new List<question>>();

// Assume document is loaded and we have the root node.
// Get the full list of question nodes.
XmlNodeList nodelist = root.SelectNodes(myQuestionPath);

// Load the questions
foreach (XmlNode n in nodelist) {
  unanswered.Add(new question(n));
}


Random selection is then straight-forward. If displaying a single question at a time then something like the following might serve.
C#
while (unanswered.Count > 0) {
  int qNo = (new Random()).Next(unanswered.Count+1));
  question = unanswered(qNo);
  unanswered.RemoveAt(qNo);
  answered.Add(question);
  // Display question and prompt user to respond.
  getUserResponse(question);
  }

// Now work through the answered questions totalling correct answers.
 
Share this answer
 
v2
Thank you for your answer. But i still don't understand XML files :( I look some guides or web sites , but i don't understand.

OK - try starting here : https://en.wikipedia.org/wiki/Well-formed_document[^]

XML
<question>
  <number>1</number>
 
  <options>
    <option> 
      <number>1</number>
      <optionText>option 1 text</optionText>
    <option>
    <!-- As many options/possible answers as required -->
    <option> 
      <optionNumber>n</optionNumber>
      <optionText>option  n text</optionText>
    <option>
  </options>
 
  <!-- Allow for questions with more than one correct response -->
  <responses>
   <response>1</response>
  </responses> 
 
</question>


Is this XML code for only one question?
What is the Options and Option blocks?
Can you organize your XML code to my code(only one question) please? It helps me a lot...

Yes. That is the layout for one question.
The <options> block contains all the text of possible answers to the question.
I missed <text> element to hold the the question text. See the example below.

A concrete example.

XML
<question>
  <number>1</number>
  <text>Aberdeen is a city in which country?</text>
 
  <!-- Give the user a number of choices -->
  <options>
    <option> 
      <number>1</number>
      <optionText>Scotland</optionText>
    </option>
    <option> 
      <number>2</number>
      <optionText>United States of America</optionText>
    </option>
    <option> 
      <number>3</number>
      <optionText>Nepal</optionText>
    </option>
  </options>
 
  <!-- 
      Now list the numbers that correspond to options which give
      the correct answer or answers.
  -->
  <responses>
   <response>1</response>
   <response>2</response>
  </responses>
 
</question>



On the other hand, i don't understand some parts of your codes, which are;

C#
while (unanswered.Count > 0) {
    int qNo = (new Random()).Next(unanswered.Count+1));
    question = unanswered(qNo);
    unanswered.RemoveAt(qNo);
    answered.Add(question);
    // Display question and prompt user to respond.
    getUserResponse(question);
    }


In this code,is "getUserResponse" is a separate function,if it is what is inside this function?

It's just pseudo-code to show where you would write code to display the question and get the user's answer(s).

C#
List<question< unanswered = new List<question<();
List<question< answered  = new List<question<>();

// Assume document is loaded and we have the root node.
// Get the full list of question nodes.
XmlNodeList nodelist = root.SelectNodes(myQuestionPath);

// Load the questions
foreach (XmlNode n in nodelist) {
  unanswered.Add(new question(n));
}


In this code, you wrote "// Assume document is loaded and we have the root node.// Get the full list of question nodes." , in this section of code i wrote this code ,is this code right?

Probably. I didn't look closely at it. :)


C#
XmlTextReader xmlrd = new XmlTextReader(@"C:\\Users\Onur\Desktop\xml deneme.xml");

XmlDocument doc = new XmlDocument();
doc.Load(xmlrd);

XmlNode root = doc.SelectSingleNode("question");
XmlNodeList list = root.SelectNodes("options");


In this code ,first you wrote Number but you never used it, what is it?
Second, you created 3 lists which are "Options","Responses" and " Answers" , but i can't figure how to implement this lists using my XML file.

C#
class question {
   public question(XmlNode q) {/*load question from XML node*/}
   public Number {get; set;}
 
   // List of options to choose from.
   public List<option> Options {get; set;}
 
   // List of valid responses
   public List<response> Responses {get; set;}
 
   // User answers
   public List<answer> Answers {get; set;}
 }


Again this is just pseudo-code to suggest one possible way that you might hold an XMLNode in your XML file in C# class. Number would be loaded from a question XMLNode as would the question text and the options and responses. Answers will hold the user's responses.

Thank you again...
 
Share this answer
 
v2
Is this correct ?

C#
<question>>
    <number>1</number>
    <text>3/2 + 1/2 = ?</text>

    <options>
         <option>
             <number>1</number>
             <optionText>1</optionText>
         </option>
         <option>
             <number>2</number>
             <optionText>3/5</optionText>
         </option>
         <option>
             <number>3</number>
             <optionText>2</optionText>
         </option>
         <option>
             <number>4</number>
             <optionText>5</optionText>
         </option>
    </options>

    <responses>
          <response>3</response>
    </responses>    

    <number>2</number>
    <text> 1/2 + 6/2 = ?</text>

    <options>
         <option>
             <number>1</number>
             <optionText>5</optionText>
         </option>
          <option>
             <number>2</number>
             <optionText>8/7</optionText>
         </option>
          <option>
             <number>3</number>
             <optionText>7/2</optionText>
         </option>
          <option>
             <number>4</number>
             <optionText>3</optionText>
         </option>
     </options>

     <responses>
           <response>3</response>
     </responses>


     <!--   And so on... -->
</question>


Thank you so much...
 
Share this answer
 
I suppose you need to make wcf for data communication and keep getting single question from there. No need to keep data in android application.
Or if you need to keep data in app than keep say x no. Of questions only . When user finish with all answers we can get more from wcf. But here still you need to send answers to your db.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900