Use xml dom and XPath to create online tests for multiple choice questions

Source: Internet
Author: User
Tags cdata xpath contains
Use xml dom and XPath to create online tests for multiple choice questions Author:Enrico metrics ar Samuel
Source: http://www.codeproject.com/aspnet/xmlquiz.asp

Introduction
The idea of creating online tests comes from an accidental opportunity for me to look for XML tutoring courses. I visited w3school.com and found that I was more interested in an online test on this website, not just a tutoring course for searching for XML. I felt quite good when I did 20 tests at a time. I don't know why only some web pages provide such online testing services. Testing is a good way to evaluate knowledge. An online test is a good supplement to a Web page. It allows visitors to stay on the web page for a longer period of time. I downloaded the trial version and used it for a while. It is a ten-way test to enhance your knowledge about Australia's geography. The answer is written in an XML document that is transparent to ordinary people. We can easily see the answer. CodeExplanation I will not explain every detail of the Code in detail, but will focus on some of the parts. Once you understand the overall meaning of the code, you can modify or add code to meet your individual needs. Recursive code On this online test page, only one active recursive code completes multiple tasks. Recursive code can call itself repeatedly until the defined conditions are met. Accurately speaking, this code does not call itself, but only transmits the data to itself. This process is called back transmission. Because this code continuously transmits information to itself during the test, it can be said that this code has multiple States. The first State is to initialize some basic variables, calculate the total number of problems and record the Test start time. Then, in the first and subsequent states, the Code displays a multi-choice question on the page for the user to answer (see the picture above ). After a question is answered, an onclick event is triggered to push back the code and run it to the next state. In this status, the code will run a sub-program when an event occurs. Program To check the answer and display the next question. Recurrence continues until the last question is answered, and the answer result is displayed on the page. The following working chart shows the running process of the recursive flow code of this online test: Status Maintenance The code for online testing needs to maintain the status of each variable. There are many options to solve. The most advanced method is to use the session object. The most traditional method is to use input to hide or use a querystring. ASP. NET introduces a method called 'State package. Unlike the session object, the 'State package' does not always detect the entire user session process, but changes from one page to another as early as the input and querystring are hidden. However, it is more advanced than hiding input and querystring because it can accept more data types and its content is compiled into a separate string, which is not easily changed. Store the value' State bag ' Viewstate ("totalquestion") = inttotalquestion From' State bag 'Value Inttotalquestion = viewstate ("totalquestion ") The following table lists the variables stored in' State bag '.
Variable name State bagName Data Type Description
Inttotalquestion Totalquestion Int The total number of questions to save during the test. The value is assigned and saved as constant during initial state.
Intscore Score Int Save the correct question count
Intquestionno Questionno Int Save the question number for the last question answered by the current user
Arranswerhistory Answerhistory Arraylist of int Record the test answer. If the answer is correct, enter 0. Otherwise, the index number of the selected answer is recorded.
(None) Correctanswer Int Save the correct answer to the previous question. When the answer is checked correctly, the next status is available.
(None) Starttime Date Save the Test start time, which can be used to calculate the test time.
XML Data The online test data is stored in Quiz . Xml You can use XML tables. Quiz . XSD . A valid XML document contains at least one MCHOICE element in the Union of elements named quiz ). Each MCHOICE element has a question subelement and more than two answer subelements. The answer element may have the correct attribute whose value is yes or no. In fact, in the same MCHOICE, each answer should be given a Yes value of the correct attribute, otherwise there will be no correct answer to this question. Quiz . Xml : <? XML version = "1.0" encoding = "UTF-8"?> < Quiz Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: nonamespaceschemalocation =" Quiz . XSD "> <MCHOICE> <question> what is the capital city of Australia? </Question> <answer> sysydney </answer> <answer correct = "yes"> Canberra </answer> <answer> Melbourne </answer> <answer> Gold Coast </answer>/ answer> </MCHOICE> <question> which city has an extensive tram network? </Question> <answer> sysydney </answer> <answer correct = "yes"> Melbourne </answer> <answer> adeloide </answer> <answer> Ballarat </answer> </MCHOICE> </ Quiz > It is necessary to insert HTML tags for XML data so that the page contains modified documents, images, links, and other documents instead of monotonous documents. You only need to enclose the CDATA block around the HTML tag to ensure the availability of the XML document. Let's take a look at the following example: <MCHOICE> <question> <! [CDATA [Which of the following is <u> not </u> extends Alian native animals?]> </Question> <answer> kangaroo </answer> <answer correct = "yes"> penguin </answer> <answer> koala </answer> <answer> wombat </answer> </MCHOICE> Quiz . XSD : <? XML version = "1.0" encoding = "UTF-8"?> <Xs: schema xmlns: xs = "http://www.w3.org/2001/XMLSchema" elementformdefault = "qualified" attributeformdefault = "unqualified"> <Xs: element name =" Quiz "> <Xs: complextype> <Xs: Choice> <Xs: element name =" MCHOICE "maxoccurs =" unbounded "> <Xs: complextype> <Xs: sequence> <Xs: element name = "Question" type = "XS: string"/> <Xs: element name = "Answer" minoccurs = "2" maxoccurs = "unbounded"> <Xs: complextype> <Xs: simplecontent> <Xs: Extension base = "XS: string"> <Xs: attribute name = "correct" use = "optional"> <Xs: simpletype> <Xs: Restriction base = "XS: string"> <Xs: Enumeration Value = "yes"/> <Xs: enumeration value = "no"/> </Xs: Restriction> </Xs: simpletype> </Xs: attribute> </Xs: extension> </Xs: simplecontent> </Xs: complextype> </Xs: Element> </Xs: sequence> </Xs: complextype> </Xs: element> </Xs: Choice> </Xs: complextype> </Xs: Element> </Xs: schema> there are several reasons why the online test code does not violate the XML table. First, it is a mandatory resource process that forces xmltextreader to browse each element and attribute in the document. Second, we do not need to activate the XML document every time a file is loaded, as long as it is activated once after the update, you can use a separate aspx code or a third-party tool and manually run the code after each XML document update to activate the XML document. XML Document Object Template The object template (DOM) of an XML document is a tree structure that represents each XML document node based on its inheritance relationship with its parent node and child node. The object template of an XML document allows you to operate an XML document in multiple logical ways. We use the xmldocument class to create an object template for an XML document. The xmldocument class is an extension of the xmlnode class. It can inherit many properties and methods from the xmlnode class. When the nature and method of the xmlnode class are applied to a node in the XML document, the nature and method of the xmldocument class are applied throughout the XML document. The following code is from Quiz . Xml Create an instance of the xmldocument class and the object template dim xdoc as xmldocument = new xmldocument () xdoc. Load (server. mappath (" Quiz . Xml ") it is a little tricky to locate a specific node in the object template of the XML document. We need to let the pointer 'pointer 'start from the object template of the XML document through the root node. The following code shows how to locate the first problem and the first multiple options: dim xnode as xmlnode 'Goto the first/base element in the file, which is <? XML?> Xnode = xdoc. documentelement 'Goto the next sibling of current node, which is < Quiz > Xnode = xnode. nextsibling 'Goto the first child of current node, which is <MCHOICE> Xnode = xnode. firstchild 'Goto the first child of current node, which is < Quiz > Xnode = xnode. firstchild 'Print the content of current node Response. Write ("The question is:" & xnode. innerhtml) is obviously a tedious task, especially when you want to locate a low-level node. Fortunately, we can use the XPath language to locate a specific node or a group of nodes more directly. If you are not familiar with XPath, refer to the next section for a brief introduction. The selectnodes method of the xmlnode and xmldocument classes accepts an XPATH string and returns a group of xmlnode objects called xmlnodelist. Another method is called selectsinglenode to do the same thing, but only one xmlnode object is returned. 'Using selectnodes to address all answers of the first multiple choice Dim xnodelist as xmlnodelistdim I as integerxnodelist = xdoc. selectnodes ("/ Quiz /MCHOICE [1]/answer ") for I = 0 to xnodelist. count-1 response. write ("<p>" & xnodelist. item (I ). innerhtml) Next 'using selectsinglenode to select the first question of the first multiple choicedim xnode as xmlnodexnode = xdoc. selectsinglenode ("/ Quiz /MCHOICE [1]/question ") response. Write (" The question is: "& xnode. innerhtml) Xpath XPath is a language used to locate specific nodes in XML documents. It locates a node or a group of nodes through a string that describes the inheritance relationship. Therefore, this string is usually called an XPATH string. If you are familiar with file paths or URLs, the concept of xpath is not new. From the left side of the XPath string, you can point out the node it locates. Except for some situations or purposes, XPath is useful. The following table describes how XPath relies on Quiz . Xml Some Usage:
XpathString Result
/Quiz Select the XML root node containing all elements
/Quiz/MCHOICE SelectQuizAllMCHOICEChild Element
/Quiz/MCHOICE [1] SelectQuizThe firstMCHOICEChild Element
/Quiz/MCHOICE [1]/question Select all questions about the first MCHOICE of quiz
/Quiz/MCHOICE [1]/answer [4] SelectQuizThe firstMCHOICEThe fourth answer
/Quiz/MCHOICE [1]/answer [4]/@ correct SelectQuizThe firstMCHOICE'Correct'Attribute
XPath contains too many surprises. If you are interested in learning more, you can find the XPath tutoring course in w3cschools. Summary

This articleArticleThis article introduces online testing as a tool, which is a good supplement to Your webpage. It also describes some theme-like recursive code, uses the object template in the XML document to operate and control the XML document, slightly mentions the XPath language, and briefly discusses State maintenance.

Posted on. NET refermer read (2373) Comments (4) EDIT favorites reference favorites to 365key


Comment:

# Re: use xml dom and XPath to create online tests for multiple-choice questions. | Yul's article was not fully understood, but a small question occurred while reading it, how does one deal with the [Backward] rollback ??? I am a very careless participant. When I click [next], I find that I should select another answer, so I have to click [Back] and fill in again, in this case, is the East-East in viewstate returned to the previous state, or is it in a new State? Then, when I clicked [next] Again, I suddenly changed my mind and wanted to change the choice of a previous question. I kept clicking [Back], result oh, oh ~~~~ Fainted! How? Reply
# Re: use xml dom and XPath to create online tests for multiple-choice questions. | is the UML diagram of csover self-painted? Reply
# Re: use xml dom and XPath to create online tests for multiple-choice questions. |. Net javasmeruml diagram is a response to the original text.
# Re: Use the xml dom and XPath to create multiple choice questions online test |. Net javasmerre: Use the xml dom and XPath to create multiple choice questions online test | Yul
I didn't fully understand the article, but when I was reading it, I thought of a small question: How can I deal with the [Backward] rollback ??? I am a very careless participant. When I click [next], I find that I should select another answer, so I have to click [Back] and fill in again, in this case, is the East-East in viewstate returned to the previous state, or is it in a new State? Then, when I clicked [next] Again, I suddenly changed my mind and wanted to change the choice of a previous question. I kept clicking [Back], result oh, oh ~~~~ Fainted! How?

Well, these issues do not seem to be considered in this article. In the specific implementation process, this should be well solved. Thanks

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.