如何将我的XML文件中的数据导入我的Android应用程序? [英] How to get data in my XML file to my android application?

查看:69
本文介绍了如何将我的XML文件中的数据导入我的Android应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨;



我想开发一个Android应用程序,在这个应用程序中我想随机获取我的xml文件中的数据,并向用户显示这个问题。比用户回答这个问题(对或错),应用程序在屏幕上得到另一个随机问题,这将继续这样。



所以,我的问题是如何得到这个数据随机存在于XML文件中。



我尝试了一些方法来做到这一点,但这种方法无法完全按照我的要求执行。



这里的方法;



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;

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;
          }




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

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

               label1.Text = reader.Name;
           }




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;
              }
          }




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;
            }







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();
           }





这是我的XML试用文件;





And here is my XML trial file;

<?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...

推荐答案

这个循环应该实现什么?看起来它只是覆盖了上一次迭代中加载的数据。

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





我会考虑更改问题文件的结构。您将序列信息嵌入元素名称中,这将使您的xpath导航比它需要的更难。标识应该是元素的属性< myTag ID =1... />或者单独的子元素 - 最适合您的问题。例如,问题元素可能表示如下。





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.

<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 :

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:

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.

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.


感谢您的回答。但我仍然不理解XML文件:(我看一些指南或网站,但我不明白。



好的 - 尝试从这里开始: https://en.wikipedia.org/wiki/Well-formed_document [ ^ ]



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[^]

<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>





这个XML代码只针对一个问题吗?

什么是选项和选项块?

你能将XML代码整理到我的代码中(只有一个问题)吗?它帮了我很多...



是的。这是一个问题的布局。

< options> block包含问题可能答案的所有文本。

我错过了< text>用于保存问题文本的元素。请参阅下面的示例。



一个具体的例子。





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.

<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;

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);
    }





在这段代码中,getUserResponse是一个单独的函数,如果它是这个函数里面的东西?



这只是伪代码,显示会编写代码来显示问题并得到用户的答案。





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).

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));
}





在这段代码中,您写了//假设文档已加载,我们有根node.//获取问题节点的完整列表。 ,在我写这段代码的这段代码中,这段代码是对吗?



可能。我没有密切关注它。 :)







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. :)


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");





在这段代码中,你首先编写了Number但你从未使用它,它是什么?

其次,您创建了3个列表,分别是选项,响应和答案,但我无法确定如何使用我的XML文件实现此列表。





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.

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;}
 }





再次这个只是伪代码,可以建议一种可能的方法,即在C#类的XML文件中保存XMLNode。数字将从问题XMLNode加载,问题文本以及选项和响应也是如此。答案将保留用户的回复。



再次感谢...



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...


这是正确的吗? />


Is this correct ?

<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...



Thank you so much...


这篇关于如何将我的XML文件中的数据导入我的Android应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆