Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Microsoft CSharp Programming For The Absolute Beginner (2002) [eng]-1.pdf
Скачиваний:
46
Добавлен:
16.08.2013
Размер:
15.71 Mб
Скачать

Figure 10.2: The quiz interface is quite simple.

Creating and Editing Quizzes

As in the adventure game from Chapter 9, “File Handling: The Adventure Kit,” the user can edit existing quizzes and create new ones. Figure 10.3 shows the quiz editor. On the surface, the Quiz game is much like the Adventure Kit game. However, the Quiz game uses an entirely different technique for handling the underlying information. I deliberately made these two programs similar in other features so that you can concentrate on the underlying technical differences.

Figure 10.3: The user can enter quiz data using an editor, much like the Adventure Kit game.

Investigating XML

The Quiz game stores and manages quiz data using a format called eXtended Markup Language (XML). XML has become an important technology in the past few years. XML is often used to share

278

information between programs. You will likely come across XML data somewhere in your programming travels, so you should know how to work with it. Fortunately, C# and .NET provide powerful tools for working with XML data.

Defining XML

XML can be a bewildering topic because it is a wide−reaching standard and implementation tools abound. However, the basic concepts behind XML are not as complex as they might seem.

XML is a technology for describing data. An XML document is a file that contains data. In addition to the data itself, an XML document contains information about the data (often called metadata). An XML document is written in plain text that can easily be distributed over the Internet. It uses a series of tags to describe the various data elements, much as HTML uses tags to describe the parts of a Web page. The interesting thing about XML is its capability to generate new tags to describe any kind of information.

A tag is simply a word inside angle brackets (< >). Most elements in XML have both a beginning tag and an ending tag. A tag can contain text (or other types of data) or other tags.

XML is closely related to HTML, the language of Web pages. XML and HTML share a common ancestor, SGML (Standard Generalized Markup Language), and they demonstrate a strong family resemblance. If you write any HTML code or look at the code behind a Web page, you will find XML to be very similar in its general design. HTML is designed for only one specific job—to format Web pages. XML is designed to let you describe any kind of data you want.

In the Real World

In this chapter I am focusing on how you can use and define your own custom version of XML, but interesting and powerful predefined XML languages are available. Most of these languages are designed to simplify working with a particular type of information or data. Some interesting XML languages are

SMIL (Synchronized Multimedia Integration Language). This language defines multimedia presentations. It is used to generate slide shows, synchronize captions with streaming audio and video content, and manage other kinds of multimedia information.

SVG (Scalable Vector Graphics). This language allows authors to create vector−based graphics, which are often much more compact and flexible than traditional graphics schemes. It is supported by an international standards body, the W3C (World Wide Web Consortium), but not by Microsoft.

VML (Vector Markup Language). This language is Microsoft’s answer to SVG. It is another powerful, vector−based system for creating graphics in documents and Web pages.

CML (Chemical Markup Language). This language is dedicated to simplifying the drawing of chemical figures, which has long been a challenge for those who frequently write about chemistry.

SOAP (Simple Object Access Protocol). This language enables objects to communicate across the Internet.

An example will help make sense of all this theory. The quiz program you saw at the beginning of the chapter is based on a form of XML I invented just for this chapter. (That’s the fun part of XML—you get to invent new languages.) My new language will describe a quiz. Take a look at the XML code that describes the sample test:

279

<?xml version="1.0" encoding="utf−8"?>

<test>

<problem>

<question>What is the primary language of this book?</question> <answerA>Cobol</answerA>

<answerB>C#</answerB>

<answerC>C−−</answerC>

<answerD>French</answerD>

<correct>B</correct>

</problem>

<problem>

<question>What does XML stand for?</question> <answerA>eXtremely Muddy Language</answerA> <answerB>Xerxes, the Magnificent Chameleon</answerB> <answerC>eXtensible Markup Language</answerC> <answerD>eXecutes with Multiple Limitations</answerD> <correct>C</correct>

</problem>

<problem>

<question>Which command sends a message to the user?</question> <answerA>Messagebox.Show()</answerA> <answerB>sendMessage()</answerB>

<answerC>Alert()</answerC>

<answerD>HeyUser()</answerD>

<correct>A</correct>

</problem>

</test>

By examining the quiz code, you can see that it defines a structure for the quiz. The first line of the code describes the file as XML code and defines the encoding type:

<?xml version="1.0" encoding="utf−8"?>

Almost every XML file you see begins with a similar line. The utf−8 encoding refers to the Unicode text−formatting scheme used by C# and most other modern languages to support international languages. The line begins with <? and ends with ?> to indicate that this is a special header line. It is required in most XML documents.

Trick The terms version and encoding are attributes of the XML tag. Each tag can have attributes that modify the data in some way. For example, the <img> tag in HTML has attributes to modify the width and height of the image. If you design an XML document, your tags can have attributes. Working with attributes is easy, but to keep things simple in this introductory chapter, I decided to use them only where they are mandated, as in the XML tag in the preceding code.

If you examine the quiz’s structure, you will quickly see some patterns emerge. The document is composed of nested structures. The bulk of the document is encased inside a <test> </test> pair. Inside that are a series of <problem> </problem> sets. Each problem consists of a <question>, several <answers>, and a <correct> element. You might also see the structure as a hierarchy, as illustrated in Figure 10.4.

280

Figure 10.4: The test contains problems, which can contain a question, answers, and the correct answer.

It’s important to note that the tags describe what kind of data is represented, rather than how data is to be presented on the screen. An important aspect of XML is how it focuses on the meaning of the data alone. XML data is intended to be used by many programs written in different languages on different platforms, so it is up to the program that uses the data to determine how exactly the data will be displayed.

In the Real World

The original intent of HTML was to describe only the meanings of various text elements in the context of a Web page rather than to determine how a page is to be depicted on the monitor. (In fact, a tag such as <i> is considered rude by HTML purists, who prefer the <emphasis> tag because it describes what kind of text is being encased rather than how to display it.) HTML documents are

281

meant to be displayed on many kinds of devices, including cell phones and PDAs, which cannot always handle all the complex formatting commonly indicated on modern Web pages. For this reason, it is still smarter to use HTML to determine what the text means rather than how it is displayed. It is considered more appropriate to use style sheets to determine exactly how the data is to be displayed. XML documents can be displayed using style sheets, also, but programmers can do even more. When you learn how to read and manipulate an XML document in this chapter, you will be able to display XML data however you want.

Learning XML Syntax Rules

You must follow a set of rules when creating an XML language. In this sense, XML is stricter than HTML. However, these rules are very easy to learn and allow tremendous flexibility.

All Tags Are Lowercase

In HTML, you can use <center> or <CENTER>. In an XML language, you are required to use lowercase letters for any tag you define. However, to separate words, you can use mixed case, such as <myTag> or <questionNumber>.

All Tags Have an Ending Tag

If you define a tag, it must have a corresponding ending tag. The ending tag is just like the start tag, except for a slash (/) inside the angle brackets. For example, you can define a <note> tag, but you must also have a </note> tag. Tags that do not usually have an ending tag (such as the <img> tag in HTML, which defines an image) can have a slash at the end of the tag. For example,

<img src = "myImage.gif"/>

is legal in XML, but

<img src = "myImage.gif">

is not legal.

All Attribute Values Are Enclosed in Quotes

A tag can include an attribute to modify the tag’s meaning. For example, in the test data, you can have multiple−choice questions, short−answer questions, and true−or−false questions. You can extend the problem tag to indicate the type of question. For example, you could write

<problem type = "mc">

to indicate a multiple−choice question

<problem type = "tf">

or to indicate a true−or−false question.

In this case, type is an attribute, and the value of the type attribute could be mc or tf.

282

Соседние файлы в предмете Программирование на C++