LOGOUT
HELP
Richard Mansfield
Printing This Lesson
Select what you’d like to include when you print, and then click the Print Lesson button:
Text, images and activities (IE users only)
Text and images
Text only
Saving This Lesson
For instructions on saving this lesson (shown below), please select the browser you're using.
Firefox
If you'd like to access the lessons of this course when you're offline, as well as after the course has ended, it's easy to download them. First, you may want to create a folder to put all the lessons in so you have them in one place at the end of the course. Next, just follow the downloading instructions below.
Choose Save Page
Use Web Page, complete as the file type
Viewing Downloaded Lessons
After you've downloaded a lesson, you can use these steps at any time to open, view, or print it. You don't need to be connected to the Internet.
Icons for one lesson saved as Web Page, complete
Missing Pictures
If you open a downloaded lesson and some pictures are missing, you might not have waited long enough for the pictures to download before clicking Save. Interactive content, such as videos or games, may not save. Another possibility is that you may have downloaded using one browser (like Firefox) but opened using a different browser (like Internet Explorer). Try opening the downloaded lesson with the same browser you used to download the lesson (please see step #4 above).
Introduction
Hello, and welcome to Introduction to XML! I'm so happy to have you in my course.
XML is today's most popular way to store and send information. Are you interested in learning how to use it? Do you want to find out how to write simple computer programs to manage XML? Then you've come to the right place!
You don't need any technical knowledge to master this course. It doesn't matter if you haven't written a single line of computer programming. When you finish these lessons, you'll have a firm grasp of the essentials of XML, and you'll know how to write basic computer programs too.
My name is Richard Mansfield, and I'll be your instructor for the next six weeks. I've written quite a few books on programming, including coauthoring XML All-In-One Desk Reference for Dummies.
People tend to think that programming is complicated. I did too. Before I tried it. Of course, like most things, programs can be complicated. But they can also be simple and easy to write—especially when you use the great tools you'll download during this lesson.
One of the best things about XML is its flexibility and the freedom it gives you. It's not some predefined format or set of labels that somebody else created. You're in charge of how you organize and label your information.
Another cool feature of XML is that it can store information in a simple, understandable way. This makes it easy for you to write programs to manage that information—and you can write these programs quite quickly.
True, XML includes some sophisticated techniques. We'll experiment with a few of the most useful of these advanced skills in later lessons. But in general, XML is very straightforward. For one thing, you write it in plain English! (Or you can use any other human language that you might prefer.)
But best of all, XML and programming can be lots of fun.
To people who don't program, you'll seem like a computer wizard when you show them the program you create in this course. Even better, you'll be able to easily transform the program to handle any other kind of information, even pictures.
For example, let's say your friend wants a custom photo album program that'll show snapshots and details about each coin in his collection. He also wants to search the album by date purchased, condition, or any other info. Rather demanding, isn't he? But no problem. You'll be able to hand over the completed program in a matter of days! Wizard indeed.
What to Expect in This First Lesson
I think you'll enjoy this lesson a lot. You'll get hands-on experience writing your first XML document, and you'll explore the features that make XML so popular. Finally, you'll see what happens when you open your XML document in various popular browsers and applications like Word.
We'll begin with Microsoft's free but feature-rich programming environment: Visual Studio Express.
Visual Studio, or VS as we'll be referring to it, is excellent for creating XML documents and programs. We'll be using it throughout this course, and it will meet your programming needs for years to come.
VS is to programmers what Word is to writers: an enormous suite of time-tested tools, including many that you'll rarely if ever need. But it's nice to know they're there anyway, just in case you're someday faced with an unusual task.
Which would you prefer? A small toolbox with one screwdriver, or a well-stocked workroom with every tool even an expert could wish for? I'd take the workroom, especially if they're giving it away for free.
Lesson 1: Creating Your First XML Document
Hello. My name is Richard Mansfield, and I'll be your instructor as you master the elements of XML, today's most popular way to store and transmit information. But there's a secret. XML stands for Extensible Markup Language, and the key word is "language."
You'll be programming your computer. You'll write three fairly sophisticated programs in this course. You'll design them to look exactly the way you want them to look, and you'll make them act the way you want them to behave. And you'll customize these programs. For example, we'll create a quiz that you can put any kind of information into so you could test a friend on the driver's test or you could quiz your children on geography or English, whatever. We'll also write a recipe book collection and a coin collection notebook, which you can easily transform into any other kind of collection: cars, stamps, anything. These programs will include your choice of information, even photos.
So as you'll see in this course, programming can be both fun and profitable. And you'll be amazed at how easy programming can be, thanks to modern drag-and-drop construction, the Internet, which contains thousands and thousands of sample examples, and XML's clear and efficient approach to information storage. I'm sure you'll have a good time. And I'm looking forward to meeting you in the Discussion Area as we progress through this course.
So thank you for joining the course, and happy programming.
END TRANSCRIPT
Ready to get started? I'll meet you in Chapter 2.
Get a Great Programming Editor
You write XML in ordinary human language, so you can create and modify XML documents in Notepad or any other plain text editor. True, these simple editors save .txt files by default, although it's easy to make them into .xml files. All you have to do is change the file name extension from .txt to .xml.
But why not use one of the best programming editors around? VS has special tools to help you efficiently write XML and programs to manage XML.
Which Version Should You Choose?
It's best to use the Visual Studio Express 2015 for Windows Desktop version. It works with Windows 7, 8, or 10. If you're using a pre-Windows 7 computer, you can download Visual Basic Express 2010, which works with Windows XP and Vista. All the programming code throughout this course will work exactly the same in the VS 2010, 2013, or 2015 versions. However, you'll face a bit of extra work when setting up the 2010 editor later in this lesson, because menus and other features differ somewhat from the later versions. If you have any questions, just ask them in the Discussion Area, and I'll be glad to help.
To get a copy of VS 2015, go to this Web page:
https://www.visualstudio.com/en-us/products/visual-studio-express-vs.aspx
Got it? Great! Let's walk through the steps of the download together.
Installing the Earlier 2010 or 2013 Versions
Note: If you prefer to use the 2013 version of Visual Studio, that's fine, too. You can download it here:
http://www.microsoft.com/en-us/download/details.aspx?id=44914
You can simultaneously have 2010, 2013, and 2015 versions on the same computer if you wish. They're all free, and they don't interfere with each other.
Note to XP and Vista users: If you're using Windows XP or Vista, you can download the Visual Studio 2010 .iso file from here:
http://go.microsoft.com/?linkid=9709969
You'll find Microsoft's instructions on installing from this .iso file here:
https://www.microsoft.com/Licensing/servicecenter/Help/FAQDetails.aspx?id=134
Help Installing VS
Remember, if you need help installing VS, or if you have questions about anything else in this course, stop by the Discussion Area. I visit the Discussion Area daily, so just ask away. Most likely, your question will be on another student's mind too, so don't be shy.
Editor ready for your first project
Registration and Quick Launch
If the editor now asks you to register, go ahead. It doesn't cost anything. In some versions, you simply sign in to your Microsoft Life account. In other versions they give you a registration key, and you just paste it into the dialog box and click the Register Now button. If you're not asked to register, do it anyway by clicking Help > Register Product.
Customizing the Editor
When you first start VS, you'll see various links and announcements. Don't worry about these. We're going to dive right in and start our XML project.
This is probably a good time to mention our project for this course. You guessed it—a cookbook. Throughout this course, you'll be creating your own personalized cookbook with recipes stored in the XML format. I'll give you some more details in the next chapter, but for now, take a look at the editor window:
Don't worry if your editor doesn't look exactly like the one shown here. We'll fix it in a minute.
What do we need, and what can we hide? We don't need to use the Solution Explorer just yet. It's most useful for larger programs.
Leave the Toolbox. We'll use it a lot in later lessons. And we'll also want to view the Error List window. If there's a problem in your XML code, this window identifies it for you by listing its line number. That way, you'll know where the error is. And the Error List also tells you what the problem is.
Here's how your editor should look after you've finished making these adjustments:
While we're at it, let's customize the editor just a little bit more. First, you'll want to turn line numbering on, because the Error List refers to line numbers.
If you don't like Consolas, the default font, you can change it, too. I changed it to Courier New for this course. Go to Tools>Options>Environment>Fonts and Colors.
And you're done! Click OK to close the Options dialog box.
In the next chapter, you'll create your first XML document!
Creating Your First XML Document
We've now got the editor in great shape—personalized to suit our needs. The workbench is clean with only the few tools we'll need for our project.
So now you're ready to open the XML editor and write your first XML code. Let's get started!
Click File > New File, and then double-click XML File in the New File dialog box. You'll see the VS editor transform into an XML editor, and then the editor opens a brand-new XML document.
The editor automatically names your new file XMLFile1.xml and displays this name on the yellow tab above the editing window. The editor also inserts a line of code:
<?xml version="1.0" encoding="utf-8"?>
This is just a preliminary housekeeping task, because most XML documents start like this.
The Excitable Error List
Did you notice the error and that alarming red circle ? What's up? What have we done?
It's not our fault—and it'll disappear from the Error List when we type in our first XML element in a minute. The VS Error List tool is like an extremely diligent but tightly wound teacher. It's often very helpful, but sometimes it freaks out. It's always watching the code, and it can be a bit overcareful, like this warning about a missing root level element. You just created this new file, so you haven't had time yet to add the root element.
In other words, don't worry.
This first line in an XML document announces and specifies a few things:
Building Your Personal Database
Okay, so good so far. As mentioned, you'll be creating a personal cookbook in this course—a collection of recipes stored in the XML format. And we'll write our own XML app to access and manage the recipes. You'll even be able to search for anything—leftovers, Mexican, heart healthy, microwave, tomato, 2015—whatever. It'll be like your own personal Google.
What's more, XML is so flexible that the cookbook project is easy to modify. Later in the course, I'll show you how to quickly change your project into various apps to store, organize, and display pretty much any kind of information.
This will be your project. You build this app from the ground up. So you can always change what you store in it and how you display it.
I'm sure you've noticed little annoyances (and some not so little) when you use commercial apps. One of my big issues with Word is that they've "streamlined" it. Its current designers think Word should display as little information as possible. For example, if you search a document, they show you a list of the results. That's great, but the minute you try to edit the document, they hide the results list. Why? You sometimes want to look at that list.
To me, this is extremely unhelpful. So I wish I could get into Word and delete the programming lines that do this.
One of the coolest things about programming—about writing your own program as we'll do in this course—is that you can totally customize it: what it looks like, what it does, how it does it. In other words, you tell the machine how to behave.
So let's get started. We'll begin by describing the structure of our data. By structure, I mean how we want to organize and label our data. What kinds of information do we want to include? Stop and think a minute about the elements of a simple cookbook. It's a collection of recipes, so what are the main components of any recipe?
Nesting Elements
For our cookbook, let's keep it really simple at this point. Each recipe will have only two elements: its title and its instructions.
XML stores information in nested elements: elements within elements.
XML is like a set of nested tables. Our cookbook has a parent element, the <recipe>. And nested within each parent recipe are the two child elements—the <title> and the <instructions>.
To alert you when a word is part of a programming language like an XML element, we're using the Courier typeface to distinguish it from the rest of the text. This makes it easy for you to tell that the tag <recipe> is part of an XML program, but that recipe refers to an ordinary recipe that's not a code word.
You could come up with other structures that would work fine. And you could use different names for your XML elements too:
<name>, <ingredients>, <procedure>
Remember, with XML, you're in charge of organizing and labeling things how you want.
But for our cookbook, let's stick with <recipe>, <title>, and <instructions>. That way we're all building the same project:
<cookbook> <recipe> <title> <instructions>
Each XML element must start and end with the same word, called a tag. So the tags for our XML structure look like this:
[*START] <recipe> <title></title> <instructions></instructions> </recipe> [*END]
Notice that end tags always have a slash ( / ) character.
This labeling with tags is quite similar to the way you'd describe the information on a business card:
Ms. Catherine Sloper
Founder: The Morris Townsend Memorial Trust
(212) 890-123(555) 890-1234
(212) 890-1235(555) 890-1235
(212) 890-1236(555) 890-1236
1 Washington Square, NYC
But these phone numbers are confusing because they have no identifying labels. Which one is the FAX number, for instance? So we label them on a business card like this:
(212) 890-1234(555) 890-1234 Home
(212) 890-1235(555) 890-1235 Fax
(212) 890-1236(555) 890-1236 Cell
XML takes this a little further, labeling both the start and the end of every piece of information, like this:
<name>Ms. Catherine Sloper</name>
<title>Founder: The Morris Townsend Memorial Trust</title>
<home_phone>(212) 890-1234(555) 890-1234</home_phone>
<fax>(212) 890-1235(555) 890-1235</fax>
<cell_phone>(212) 890-1236(555) 890-1236</cell_phone>
<address>1 Washington Square, NYC</address>
XML tag names can even include numbers or symbols like%*!#. But there are four rules:
Auto-Completion
Among the VS editor's many helpful tools is a feature called auto-completion. That's a fancy way of saying that it completes code lines for you. You type in the first half of an element, the start tag. But the editor knows that every XML start tag must also have a matching end tag. So the editor types in all the end tags by itself. Let's see how this works.
We'll type in the root level element that the Error List was upset about earlier. This is the first element. It contains all the other elements, so its two tags appear only at the start and end of the entire document. (It's called the root because structures like XML's are sometimes described as tree structures: They start out with the root, then the branches, then twigs, then smaller twigs. This is just another way to visualize nested elements.)
As soon as you type the greater-than symbol (>) to finish the start tag, the editor automatically adds the end tag </cookbook>. You get the whole XML element but only have to type in the start tag. Pretty nice.
Editor Reminds You to Save Your Work
The editor also visually reminds you to save your work. When you typed in the first character on line 2, the less-than symbol (<), your document was changed (so it probably should be saved at some point). The editor does two things to remind you that you've modified this document: A yellow bar appears to the left of the changed line, and an asterisk (*) appears next to the filename on the yellow tab, like this: XMLFile1.xml*. Both of these cues show that you've modified, but not yet saved, this code file. The yellow bar means an unsaved line, and the asterisk signals that the whole file is unsaved.
Press CTRL + S to save the form you're working on. Or click the disk icon just below the menus. When the file is saved, the yellow bar changes to green.
Adding a Second Element
After the editor auto-completes your element, it positions the insertion cursor right between the start tag and end tag of the cookbook element. The cursor is blinking at you and waiting for your next move. This is exactly where the cursor should be for you to add your next element, recipe. You want this first recipe in between the two cookbook tags. (In fact, all of the recipes will be between the root cookbook tags.)
Again auto-completion kicks in and adds the end tag, </recipe>. The editor also helpfully indents the recipe element to illustrate that the recipes are children, nested within their cookbook element parent.
So at this point, your code looks like this:
[*START] <cookbook> <recipe>|</recipe> </cookbook> [*END]
Now it's time to finish the cookbook's structure by adding the final two elements, title and recipe.
[*START] <cookbook> <recipe> <title>|</title> </recipe> </cookbook> [*END]
The <instructions></instructions> element isn't nested inside the title. The <instructions> are part of the recipe, not the title.
[*START] <cookbook> <recipe> <title></title><instructions> </recipe> </cookbook> [*END]
These two elements—title and instructions—are alike. They're on the same level in the hierarchical XML structure. They share the same parent. Technically, they're called siblings.
This is somewhat hard to visualize at first, but the giveaway is that the editor indents siblings the same amount under the parent they share. Like this:
All right, so you know the main relationships between elements in XML: parent, child, and sibling.
If your code looks different, edit it to fix it.
That was easy, wasn't it? Thanks to the editor, you only had to type half the tags, and you didn't need to do any of the indenting.
Good work. We've defined the cookbook's structure, the template for each recipe. So now it's time to enter some actual content.
Let's Chat!
To keep things simple, we're using only two elements for each recipe in this lesson. But can you think of any additional recipe elements that would be useful in a cookbook program? Pop into the Discussion Area to share your ideas. This is a great way to get to know your classmates!
Filling in Content and Viewing the Project
Before we get started adding content to our template, try your hand at this quick game to see how much you remember so far.
Matchbox Game
Ready? We're going to begin filling in the content, the actual data, by specifying your first recipe's title.
Great. Now, the instructions for this recipe are lengthy, so instead of typing everything into the VS editor, just copy and paste it from this lesson.
Drag your mouse to select the instructions, and press CTRL + C to copy it.
Click your mouse to position the editor's blinking insertion cursor between the >< symbols in the center of the <instructions></instructions> element.
Press CTRL + V to paste the text in.
Here's the content to put between the <instructions></instructions> tags:
To prevent the slaw from getting runny or turning grey, mix 1 Tablespoon of kosher salt into the shredded cabbage, and let it sit for 1-4 hours. You're briefly pickling it. Then, rinse the cabbage in a colander, and gently press it to drain it. Roll gently in paper towels to dry it further. Now add your dressing. Most restaurants know all this. Most home cooks don't. Keeps well for a week!
Adding more recipes to this XML cookbook document will be even easier than this first one. Why? The structure doesn't change, only the content (the actual recipe names and directions). So you never need to type in the tags again—just copy and paste the same template for each new recipe.
Remember that when entering content, you don't have to worry about uppercase and lowercase letters. You can freely capitalize any way you want.
But XML tags are case sensitive, so XML considers <Recipe> and <recipe> two different tags.
Some experts think it's a good idea to always stick with lowercase letters so you can avoid certain kinds of errors. We'll follow that good advice in our project and never use uppercase letters in our tags.
Saving Your Work
Before trying a few experiments with our brand new XML document, let's first save our work. Remember that the editor automatically named your first new XML file XMLFile1.xml. If you open a second XML file, the editor will name it XMLFile2, and so on.
But these default names aren't too descriptive are they? So let's save our file as recipes.xml.
Also, Visual Studio will try to save recipes.xml in its VS default folder. But we want it in the Cookbook folder with the rest of our project.
Now that you've saved your XML file, you can close Visual Studio. Just click the X in the upper-right corner of the VS window.
Viewing the Cookbook Project
Most applications now use XML to store, manipulate, and transmit information. But it's also the dominant format for information on the Internet. So let's play around a little to see how some applications and browsers deal with the XML file we just created.
We'll start with the Internet Explorer (IE) browser.
The result looks pretty much as it does in the VS editor: You see the raw XML code.
Other browsers display the tags too. But Chrome, Firefox, and Maxthon add a comment: "This XML file does not appear to have any style information associated with it. The document tree is shown below."
Although browsers refuse to make a guess at how you want to display your XML data, some applications do take a stab at it. If you open an XML file in Excel or Word, you'll see that they style it as best they can—hiding the tags and even doing some simple formatting.
You don't need to actually open the XML in Word, Excel, or Access unless you want to. I'll show you what happens in these next few examples.
If you open recipes.xml in Excel, it asks which of three renderings you prefer: a table, a workbook, or a task pane.
Word's different. Word does strip off the tags, and it also displays each element as a separate paragraph. If you choose File > Open and then load in the recipes.xml file, Word first cautions you that your file includes "custom XML elements" (meaning you have tags like <recipe> and <title> that Word doesn't understand). Word also warns you that if you save this file, Word will permanently remove these so-called elements of yours. Word can be so harsh!
Attempt to open the file in Access, and the application gets in a twist. It totally refuses to display XML files in any format, raw or rendered. At first glance this is surprising because Access specializes in storing data, just as XML does. But Access clings to old, proprietary database technologies that XML is (more or less) replacing.
To be fair, Access does have excellent data analysis tools—something that XML is still working on. But try to open recipes.xml in Access, and the program tells you that this is an unrecognized database format. The only way to import recipes.xml into Access is to write a custom program to translate your XML into what Access thinks is the proper structure.
Take a look at this video, and I'll show you what it looks like when you try opening the cookbook in these different applications:
Hi. In this lesson you saw how to create an XML file and then save it to the hard drive. Let's see what happens if we load that file into Internet Explorer. All you need to do is go up to the address bar, but instead of typing in a web address, you type in the address or path on your hard drive where the XML file is located. In our case, that's C:\XML Projects\Cookbook. So let's take a look and see what happens.
Well, this isn't really what we're after is it? We don't want to display to the user all these tags, especially the weird tag at the top. What we want them to see is just the content. In other words, the title and the instructions. We don't need to show them all this. In future lessons we will learn how to get rid of the tags and also to add all kinds of other elements, such as formatting the headline, adding pictures, or whatever. For now, let's just leave this and take a look and see what happens if we load the same file into Word.
In this case, you just open it as an ordinary file, and, whoa, wait a minute. Look what Word does. It warns you—I would say almost threatens you—that these elements in your file will be removed permanently. Don't worry about that. That only happens if you save this back to the hard drive, which we're not going to do. But let's see what Word is doing with this. It's actually better than Internet Explorer for our purposes, because it shows us the title and the instructions, but none of the tags, so that's a good step in the right direction for us.
Summary
Congratulations. You downloaded and explored what is possibly the greatest bargain in software today, Visual Studio Express! You learned the basics of XML, and you experimented to see how various applications displayed your XML document by default. And it wasn't pretty, was it?
Fortunately, XML has facilities to present your information in far more effective ways, and you'll explore them in future lessons. But first, an XML challenge!
In the next lesson, you'll see how XML works with HTML, the primary language of the Internet. You'll also experiment with some cool XML techniques and add features to the cookbook project. You'll even write some computer programming.
I look forward to seeing you in Lesson 2!
Next Steps
Okay, you've finished your first lesson. Now what do you do?
You'll want to take the following steps, in any particular order you like:
This is an excellent overview of VS and its many facets. Be sure to check it out, especially if you have little or no experience programming.
Here's the link to the Visual Studio that we discuss in Chapter 2 of this lesson.
Q: After I installed Visual Studio Express, Microsoft sent me an email offering to let me join Visual Studio Online. Should I? A: You probably won't need this service. It's basically a way to store your VS projects in the cloud (on remote computers that you can connect to over the Internet, as opposed to storing your projects on your local hard drive). True, the cloud offers three major benefits:
In spite of these benefits, and even though it's free for up to five team members, I suggest you wait until you've finished this course to determine whether you'd benefit from VS Online. Joining now will just add a layer of complexity to our lessons, and we want to keep things as simple as possible.
Q: What is W3Schools, and should I take a look around the website? A: If you Google the term XML, you'll see multiple links to W3Schools. A Norwegian software and consulting company runs this website. (It isn't affiliated with W3C, the World Wide Web Consortium, the official organization that sets standards for the Internet.)
The W3Schools site offers various tutorials about XML as well as other Internet-related technologies. But again, I suggest that you wait until you finish this course before diving into more information from the W3C. They focus on using XML with browsers, but in this course we're more widely focused, and we'll also see how to use XML with ordinary applications. More important, though, some W3Schools material is inappropriate and confusing for a beginner. So I suggest you wait before you investigate too much!
Q: Why are computer programs called "code"? A: Nobody really knows how this got started. And some computer languages do indeed include puzzling abbreviations, weird phrases, and odd punctuation. Such programming can be hard to read and understand. So those languages truly do deserve the name code.
But in this course we'll use a clear, human-friendly language called BASIC that strives to be as close to ordinary English speech as possible. And even though it can do everything other languages can do, BASIC was created specifically for beginning programmers—to make programming easy to learn.
Q: Why are child elements indented?
A: Indentation is just for your benefit. It helps you see the nesting structure at a glance:
A computer looking at XML doesn't care how you format the elements. The order of the elements matters, but not the formatting.
The computer looks for the < and /> symbols to figure out the structure of your XML document. But the computer ignores any tabs, blank spaces, or even new lines of text (inserted when you press ENTER). Put another way: The computer disregards what's called whitespace in your code.
Let's say that you edited the XML so it looks like this:
<recipe><title></title><instructions></instructions></recipe>
That would be just fine as far as the computer is concerned. It would process the above code exactly the same way as this formatted version:
<recipe> <title></title> <instructions></instructions> </recipe>
But don't you agree that it's much easier for us humans to read XML code with each element on a separate line and with the child elements indented? That's what the indents are all about!
For today's assignment, you're going to add a second recipe to the recipes.xml file. To add a new recipe, you first must open your recipes.xml file in VS. There are three easy ways to open it:
For each new recipe that you add to the XML file, you'll need to use the same <recipe> structure that we created earlier in this lesson. So your first job is to copy and paste this structure in the VS editor. Follow these steps:
Remember that this cookbook element is the root element, so you have to insert everything between <cookbook> and </cookbook>.
Here's how your XML should look after adding a new recipe:
Now close VS by clicking the X in the upper-right corner of the VS window.
Back to top