Professional Documents
Culture Documents
Composite DefaultAll-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter 8
XML and C#
Documentation
CHAPTER
8
In this chapter, you will
• Be introduced to Extensible Markup Language (XML)
• Learn to create a well-formed XML document
• Discover how XML is used to create C# documentation
This is another topic that could fill an entire book. Extensible Markup Language (XML)
is an exciting new addition to the list of markup languages, and it redefines the way we
transfer data. We will take a brief look at XML and apply that information to the type
of documentation that you can create in your C# programs by using documentation
comments.
Although the exam may not contain any specific questions on creating C# documen-
tation, it will expect that you understand XML. To that end, this chapter will deal with
the basics of the XML language, including the rules for creating a well-formed XML doc-
ument. This information will be revisited in Chapter 11, where you will find out how
XML fits into the big picture. So let’s move on and discover the newest standard for ex-
changing data—Extensible Markup Language.
Introduction to XML
In the olden days of computers (which, as you know, in most cases means yesterday),
data exchange over the network and between applications happened in two ways. You
either created a binary file that included formatting code for a specific application (such
as a Word document) or you produced a straight text file that could be read by any text
editor (such as Notepad). (Technically, of course, these are both binary files—but a doc-
ument that includes formatting code cannot be read by a straight text editor.) The ad-
vantage of a binary file is that the formatting code is in binary, which makes it faster for a
computer to interpret; thus a Word document opens quickly once it reads the format-
ting information (which we could call metadata, or data about data). However, we are
limited to sending such data only to those applications that understand the metadata.
Enter SGML—Standard Generalized Markup Language. This was the first attempt to
create a format that allows a document to be self-describing. In other words, the text is
P:\010Comp\All-in-1\443-6\ch08.vp
Friday, August 23, 2002 4:58:18 PM
Color profile: Generic CMYK printer profile
Composite DefaultAll-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter 8
What Is XML?
XML is a standard that was created in order to represent data in an easily readable,
self-defining, and structured approach. It is a markup language that uses tags to define
the structure of the data that is included in the document. There are many advantages to
using XML over the more traditional data storage and retrieval methods:
There are some disadvantages to using XML, but they are being more easily overcome
as the industry swings towards its use. For example, one drawback that may have come
to mind already is that while we might describe an employee record using XML in one
fashion; you may choose to define it in another fashion. In that case, many different
documents could wind up defining the same thing. In order to overcome this potential
confusion, the industry has started to produce vocabularies, or industry-standard ways to
describe recurring data. This allows you to produce software that is more compatible
P:\010Comp\All-in-1\443-6\ch08.vp
Friday, August 23, 2002 4:58:18 PM
Color profile: Generic CMYK printer profile
Composite DefaultAll-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter 8
PART II
and HTML:
HTML
HTML is used to format data and render it in a web browser or other application that
translates HTML. There is a fixed set of tags within HTML that have been defined so that
there is a standard way of presenting the data.
Let’s look at an HTML file and then see how it is rendered in a web browser.
<html>
<body>
<h1 align="center"><i><b>My HTML Page</b></i></h1>
<div align="center">
<center>
<table border="1" width="55%">
<tr>
<td width="16%" align="center"><i><b>Student ID</b></i></td>
<td width="17%" align="center"><i><b>First Name</b></i></td>
<td width="15%" align="center"><i><b>Last Name</b></i></td>
<td width="39%" align="center"><i><b>Program of Study</b></i></td>
</tr>
<tr>
<td width="16%">123456</td>
<td width="17%">Matthew</td>
<td width="15%">Rempel</td>
<td width="39%">Software Engineering</td>
</tr>
<tr>
<td width="16%">234567</td>
<td width="17%">Geoff</td>
<td width="15%">Hamilton</td>
<td width="39%">Mining Engineering</td>
</tr>
<tr>
<td width="16%">345678</td>
<td width="17%">Koby</td>
<td width="15%">Waldron</td>
<td width="39%">Business Administration</td>
</tr>
<tr>
<td width="16%">456789</td>
<td width="17%">Lauren </td>
<td width="15%">Waldron</td>
P:\010Comp\All-in-1\443-6\ch08.vp
Friday, August 23, 2002 4:58:18 PM
Color profile: Generic CMYK printer profile
Composite DefaultAll-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter 8
Notice that the file is divided into tag sections—<html> is the top-level tag, followed by
<body> and the rest. Each section is completed when you end it with a backslash
tag—such as </html>. Since a browser understands these predefined tags, you can
open the HTML file in any browser (see Figure 8-1). Notice that this web page contains
data elements—Student ID, First Name, Last Name, and Program of Study. However,
the data is simply contained in tags that describe how it is presented visually. There is
nothing that actually describes the data itself.
XML
Now let’s look at the same data formatted using XML tags. XML has no predefined set of
tags to describe the student data we have presented in the HTML example. We describe
this data ourselves in XML. Notice in the following code example that there is nothing to
specify how the data is presented to the “viewer.”
Notice that this entire file is dedicated to simply describing the data. We will discuss
the various tags and how you create a well-formed document later in this chapter. For
now, just observe the code and the output as rendered in an XML-parser program, which
in this case is Internet Explorer 6 (see Figure 8-2).
P:\010Comp\All-in-1\443-6\ch08.vp
Friday, August 23, 2002 4:58:19 PM
Color profile: Generic CMYK printer profile
Composite DefaultAll-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter 8
PART II
Figure 8-2
An XML file
opened in
Internet Explorer
P:\010Comp\All-in-1\443-6\ch08.vp
Friday, August 23, 2002 4:58:19 PM
Color profile: Generic CMYK printer profile
Composite DefaultAll-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter 8
At this point, however, we are not too interested in displaying the data—if you are inter-
ested in either XSLT or CSS, grab a book on XML rendering. Rather, we need to under-
stand how the data is described and what constitutes a well-formed XML document. We
will discuss the rules of a well-formed XML document later in this chapter.
In the next few sections, we will briefly look at each of these and then examine the rules
for creating a well-formed XML document.
P:\010Comp\All-in-1\443-6\ch08.vp
Friday, August 23, 2002 4:58:19 PM
Color profile: Generic CMYK printer profile
Composite DefaultAll-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter 8
PART II
The declaration starts with <? and ends with ?>. If you include a declaration, it must be at
the beginning of the file, and you must code the version. Everything else is optional. No-
tice that the values are enclosed in double quotation marks. You can use either double or
single quotes, but they must match. The encoding entry specifies the character set that is
used. This could be ASCII (American Standard Code for Information Interchange), which
allows for 256 characters, Unicode 8 or 16 (UTF-8, UTF-16), which allows for a far greater
number of characters and provides international characters, or it could simply be Win-
dows if you built the file in Notepad. The standalone entry specifies whether there are
other files required to render the document.
In order to embed application-specific instructions into the XML, you must provide
processing instructions. An example of this would be to include an instruction that tells
the document to apply a particular style sheet to the data:
This line tells the XML-parser program that when it reads the data, it can format it for
output according to the specifications of the style sheet.
Comments
Any number of comments can be included in an XML document. Comments, of course,
provide detailed information to the reader of the file, but no information to the actual
document itself. As a matter of fact, comments may not even be passed on to the appli-
cation parsing the document.
A comment starts with <!-- and ends with -->, like this:
Elements
We are now at the crux of the matter. Elements are the most common type of markup that
you will see in an XML document. Elements are used to describe the data and formulate
the hierarchy of the data itself.
You use a tag to define data elements. In between the start tag, <element_name>,
and the end tag, </element_name>, is the actual text or data that belongs to the ele-
ment. In our student example, we define a student’s first name as follows:
<firstname>Matthew</firstname>
P:\010Comp\All-in-1\443-6\ch08.vp
Friday, August 23, 2002 4:58:19 PM
Color profile: Generic CMYK printer profile
Composite DefaultAll-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter 8
<student>
<id>345678</id>
<firstname>Koby</firstname>
<lastname>Waldron</lastname>
<program>Business Administration</program>
</student>
In this example, there are four sub-elements nested within the top-level element, stu-
dent. We can further nest by defining a collection of students:
<students>
<student>
<!--Student elements go here -->
</student>
<student>
<!--Next student here -->
</student>
</students>
Eventually, your design will include a single top-level element. This is called the root
element. As you will see in the rules of a well-formed document, you can only have a sin-
gle root element. All other elements are children of the root element.
When designing the structure of elements, you should follow these simple rules:
Attributes
Elements can make use of attributes, which are names matched with values that belong
to an element. We could redefine our student as follows:
In this case, <id> is an attribute that further describes the element <student>. The
value of the attribute must be enclosed in quotation marks (either double quotes or
single quotes).
P:\010Comp\All-in-1\443-6\ch08.vp
Friday, August 23, 2002 4:58:19 PM
Color profile: Generic CMYK printer profile
Composite DefaultAll-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter 8
PART II
double or single quotations; they just have to match.
Well-formed means that the document complies with the XML specifications. A docu-
ment can also be considered a valid document. In that case, it must conform to either
a DTD (document type definition) or an XML schema.
NOTE DTDs are XML documents that exist within the XML file or outside
of it. A DTD defines the rules for how the document is structured, what
elements are included, the kinds of data, and any default values.
There are several things to consider when deciding whether to use an XML schema or
a DTD. A DTD is not extensible, and there can only be one for each document. The syn-
tax of DTD is not XML, and the data typing is very weak. An XML schema is XML, which
means that all of the advantages of using XML are also inherited by the XML schema.
NOTE An XML schema is made up of data types and structures and defines
the structure of the XML document.
Having said all of that, a valid XML document adheres to the rules laid down in either
the DTD or the schema. Your XML document will fit into one of these categories:
We have now looked at the very large topic of creating and using XML documents.
The next thing to do is discuss XML documentation using special comments within your
C# program code.
XML Documentation in C#
A Java program can generate HTML documentation from special documentation com-
ments inside the program itself. Microsoft Visual C# .NET has followed this example
P:\010Comp\All-in-1\443-6\ch08.vp
Friday, August 23, 2002 4:58:20 PM
Color profile: Generic CMYK printer profile
Composite DefaultAll-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter 8
P:\010Comp\All-in-1\443-6\ch08.vp
Friday, August 23, 2002 4:58:20 PM
Color profile: Generic CMYK printer profile
Composite DefaultAll-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter 8
PART II
Figure 8-4 XML documentation file generated from documentation comments
P:\010Comp\All-in-1\443-6\ch08.vp
Friday, August 23, 2002 4:58:20 PM
Color profile: Generic CMYK printer profile
Composite DefaultAll-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter 8
Figure 8-5
Visual Studio
.NET IntelliSense
P:\010Comp\All-in-1\443-6\ch08.vp
Friday, August 23, 2002 4:58:20 PM
Color profile: Generic CMYK printer profile
Composite DefaultAll-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter 8
PART II
P:\010Comp\All-in-1\443-6\ch08.vp
Friday, August 23, 2002 4:58:20 PM
Color profile: Generic CMYK printer profile
Composite DefaultAll-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter 8
Summary
This chapter has introduced you to a lot of new topics surrounding XML. As we have sug-
gested before, if you are completely new to XML, you would be wise to catch up on this
technology by reading through documentation focused on XML. It’s a data-exchange
format that you don’t want to miss. Although the exam will probably not test you di-
rectly on your knowledge of XML, you will need to understand its uses in your applica-
tions. Be sure to check in with Part III of this book on ASP.NET to explore more of the
uses of XML. You may also find that the Microsoft exams ask you one or two questions
on the documentation comments.
Test Questions
1. Which command will cause an XML file to be generated from documentation
comments?
A. csc MyClass.cs /doc:MyClass.cs
B. cscd MyClass.cs /doc:MyClass.xml
C. cscd MyClass.cs /doc:MyClass.cs
D. csc MyClass.cs /doc:MyClass.xml
2. Which line causes the following XML to be not well-formed?
<VideoList>
<tape>
<name>XML is cool!</name>
</VideoList>
</tape>
A. <tape>
B. </VideoList>
C. </tape>
D. <name>XML is cool!</name>
3. Which XML rule does the following break?
<employees>
<Employee>
<name>Kenneth S. Lind</name>
</Employee>
<employee>
<name>Marj Rempel
</employee>
</employees>
A. There must be a single root element.
B. There must be matching opening and closing tags.
C. XML is case-sensitive.
D. All attributes must be in quotes.
P:\010Comp\All-in-1\443-6\ch08.vp
Friday, August 23, 2002 4:58:21 PM
Color profile: Generic CMYK printer profile
Composite DefaultAll-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter 8
PART II
A. There must be a single root element.
B. There must be matching opening and closing tags.
C. XML is case-sensitive.
D. All attributes must be in quotes.
5. Visual Studio .NET provides a tool to generate HTML from the XML
documentation file. It is found where?
A. Tools | Generate XML
B. Tools | Generate HTML
C. Tools | Build Comment Pages
D. Tools | Build Comment Web Pages
6. Which XML line(s) generates an employee attribute?
A. <employee name="Ken">
B. <employee attribute name="Ken">
C. <employee Name='Ken'>
D. <employee attribute Name='Ken'>
7. Which of the following lines is an XML declaration?
A. <xml version="1.0">
B. ?xml version="1.0"?
C. <?xml version=1.0?>
D. <?xml version="1.0"?>
8. Why will the following XML code not be rendered by a browser?
<name>
<lastname>Dowdy</lastname>
<firstname>Howdy</firstname>
</lastname>
A. The browser is not specified.
B. The root element is missing.
C. The root element is not closed properly.
D. The firstname element is incorrect.
P:\010Comp\All-in-1\443-6\ch08.vp
Friday, August 23, 2002 4:58:21 PM
Color profile: Generic CMYK printer profile
Composite DefaultAll-In-One
screen / MCAD/MCSD Visual C# .NET Certification All-in-One Exam Guide / Rempel & Lind / 222443-6 / Chapter 8
Test Answers
1. D.
2. B. This line needs to be at the end.
3. B. There must be matching opening and closing tags: <name>Marj Rempel
4. D.
5. D.
6. A and C. You can use double or single quotes.
7. D.
8. C.
9. D.
10. B.
P:\010Comp\All-in-1\443-6\ch08.vp
Friday, August 23, 2002 4:58:21 PM