Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Beginning Visual Basic 2005 (2006)

.pdf
Скачиваний:
219
Добавлен:
17.08.2013
Размер:
14.97 Mб
Скачать

Chapter 18

Figure 18-15

2.On the next screen, you will see a list of roles for the Web site. The only role will be Admin. Click Manage for the Admin role, as shown in Figure 18-16.

Figure 18-16

606

Forms Authentication

3.Click the link for user names starting with the letter A. All of the users that match your criteria will be available to add to the Admin role. Check the box User Is In Role to add the Admin user to the role, as shown in Figure 18-17. Clicking the box adds the user to the role. After you click the check box, close the browser and see whether you can access the View Users page. You should be able to access the page now.

Figure 18-17

How It Works

When you made the change to the Admin folder, which contains the View Users page, only users that were part of the Admin role were allowed access. The tool allowed you to add the Admin user to the Admin role. Once this was done, the Admin user could access the Admin folder and therefore the View Users page.

Summar y

In this chapter, you built the skeleton for a functional Web site with security. You used built-in controls and what you learned in Chapter 17 to complete a site with no Visual Basic code. The controls were selfcontained, and the default values worked well.

607

Chapter 18

You were able to set up security using the WAT. You saw how easy it was to enforce forms authentication in ASP.NET 2.0. After you completed the work in this chapter, you should have been flabbergasted. The amount of code required to implement flexible these features in ASP .NET 2.0 was reduced to almost 100 nothing. You saw the future of Web development in this chapter.

To summarize, you should know how to:

Use the Web Site Administration Tool

Define site layout using master pages

Implement site security using forms authentication

Apply role management to site security

Work with the built-in Login controls

Exercises

Exercise 1

Change the font to appear red for an asp:label control using the Main.skin page (created in TheClub site already) for every page under the Members directory. To do this, you can change the theme attribute on every page or change the web.config file for the directory. For this exercise, change the web.config file. You have not seen the web.config file syntax for this, so I will show it to you. Add the change to the web.config file that will apply the theme to the Web forms under the Members folder. Use the code snippet here as a guide:

<?xml version=”1.0” encoding=”utf-8”?>

<configuration xmlns=”http://schemas.microsoft.com/.NetConfiguration/v2.0”> <system.web>

<pages theme=”MainTheme” /> <authorization>

<deny users=”?” /> </authorization>

</system.web>

</configuration>

The page will look like Figure 18-18.

608

Forms Authentication

Figure 18-18

Exercise 2

The Login controls you used in this chapter are fully customizable. In this exercise, you will make some a change to the look of the login control on the Login.aspx page. Change the font color of the Login control to Red by adding the tag and font color properties to the Main.skin file. Point the web.config file under the root folder to use the MainTheme. (You did this in Exercise 1 under a different directory.)

The login.aspx page should look like Figure 18-19 when you are finished.

609

Chapter 18

Figure 18-19

610

19

Visual Basic 2005 and XML

Put simply, Extensible Markup Language (or XML, as it is known) is used for exchanging data between applications. Although it has been around for some time, XML has established itself as the de facto data exchange standard for Internet applications. Today, XML is used not only on the Internet but to exchange data between many different platforms and applications.

In this chapter, you are not going to get bogged down in the details regarding XML, for example, its validation and “well-formedness.” Instead, XML is going to be introduced generally, and then you will look at its role with Visual Basic 2005. After that, you will focus on using XML inside an application.

In this chapter, you will:

Gain a deeper understanding of XML and what it looks like

Learn how to read and write XML files

Learn how to serialize and deserialize XML data

Learn how to navigate through an XML document

Learn how to change existing XML data and add new data to an XML document

Understanding XML

The need for XML is simple: In commercial environments, applications need to exchange information in order to integrate. This integration is more applicable to the line-of-business software that a company may have rather than desktop productivity applications like Microsoft Office. For example, a company may have invested in a piece of software that allows it to track the stock in its warehouse — that piece of software would be an example of line-of-business software.

Integration has traditionally been very difficult to do, and XML, together with Web Services (which are discussed in Chapter 20) are technologies designed to reduce the difficulty and cost involved in software integration. In reducing the difficulty of software integration, there is a knock-on benefit in terms of the ease with which more general data/information exchange can occur.

Chapter 19

For example, imagine you are a coffee retailer who wants to place an order with a supplier. The oldschool technique of doing this is to phone or fax your order. However, this introduces a human element into the equation. It is likely that your own line-of-business applications (telling you what products you have sold) are suggesting that you buy more of a certain machine or certain blend of coffee. From that suggestion, you formulate an order and transmit it to your supplier. In the case of phone or fax orders, a human being at the supplier then has to transcribe the order into his or her own line-of-business system for processing.

An alternative way of carrying out this order would be to get the “suggestion” that has been raised by your line-of-business system to create an order automatically in the remote system of your supplier. This makes life easier and more efficient for both you and the management of your chosen supplier. However, getting to a point where the two systems are integrated in this way requires a lot of negotiation, coordination, and cost. Thus, it is relevant only for people doing a lot of business with each other.

Before the Internet, for two companies to integrate in this way, specific negotiations had to be undertaken to set up some sort of proprietary connection between the two companies. With the connection in place, data is exchanged not only in order to place the order with the supplier, but also for the supplier to report the status of the order back to the customer. With the Internet, this proprietary connection is no longer required. As long as both parties are on the Internet, data exchange can take place.

However, without a common language for this data exchange to be based on, the problem is only half solved. XML is this common language. As the customer, you can create an XML document that contains the details of the order. You can use the Internet to transmit that order written in XML to the supplier, either over the Web, through e-mail, or by using Web Services. The supplier receives the XML document, decodes it, and raises the order in their system. Likewise, if the supplier needs to report anything back to the customer, they can construct a different document (again using XML), and use the Internet to transmit it back again.

The actual structure of the data contained within the XML document is up to the customer and supplier to decide. (Usually it’s for the supplier to decide upon and the customer to adhere to.) This is where the “eXtensible” in XML comes in. Any two parties who wish to exchange data using XML are completely free to decide exactly what the documents should look like.

This does not sound amazing, because companies in the past and even today still use comma-separated files. These files had a format and worked within the same “philosophy.” So what does XML have that the previous formats did not?

XML is a lot more descriptive, and it can be validated against a schema. A schema defines what the XML document or fragment should look like. Even without a schema, XML can potentially describe itself well enough for others to ascertain what the data is. In line with the benefits of previous file formats, XML is also a text-based format. This means that XML can be moved between platforms using Internet technologies such as e-mail, the Web, FTP, and other file copy techniques. Traditional software integration was difficult when binary data had to be moved between platforms such as Windows, UNIX, Macintosh, AS/400, or OS/390, so the fact that XML is text-based adds to making it easier to send these across platforms.

What Does XML Look Like?

If you have any experience with HTML, XML is going to look familiar to you. In fact, both have a common ancestor in Standard Generalized Markup Language (SGML). In many ways, XML is not a language, as the name suggests, but is rather a set of rules for defining your own markup languages that

612

Visual Basic 2005 and XML

allow the exchange of data. XML is not a stand-alone technology, either; in fact, a whole lot of different related specifications form what you can and cannot do with XML. Such specifications include the following (not the best late-night reading, at least if you wish to be an alert and attentive reader):

URI (Uniform Resource Identifiers): www.ietf.org/rfc/rfc2396.txt

UTF-8 ( Unicode Transformation Format): www.utf-8.com/

XML (Extensible Markup Language): www.w3.org/TR/REC-xml

XML Schema: www.w3.org/XML/Schema

XML Information Set: www.w3.org/TR/xml-infoset/

Although the specifications may not beat a book such as this in terms of format, layout, and ease of understanding, they have a whole lot to offer the XML fan. If you feel up to it, you can read more about XML after this introduction.

XML is tag based, meaning that the document is made up of tags that contain data. Here is how you might choose to describe this book in XML:

<Book>

<Title>Beginning VB 2005</Title> <ISBN>xxxx574019</ISBN> <Publisher>Wrox</Publisher>

</Book>

In XML, you delimit tags using the < and > symbols. There are two sorts of tags: start tags such as <Title> and end tags such as </Title>. Together, the tags and the content between them are known as an element. In the previous example, the Title element is written like this:

<Title>Beginning VB 2005 </Title>

The ISBN element looks like this:

<ISBN>xxxx574019</ISBN>

And the Publisher element looks like this:

<Publisher>Wrox</Publisher>

Note that elements can contain other elements. In this case, for example, the Book element contains three subelements:

<Book>

<Title>Beginning VB 2005</Title> <ISBN>xxxx574019</ISBN> <Publisher>Wrox</Publisher>

</Book>

The structure formed by elements nested inside other elements can also be represented as a tree with, for example, Title, ISBN, and Publisher as branches from the root Book. Therefore, many use terms such as node, parent, and child instead of element.

613

Chapter 19

If you were given this XML document, you would need to have an understanding of its structure. Usually, the company that designed the structure of the document will tell you what it looks like. In this case, someone might tell you that if you first look for the Book element and then the Title element, you will determine the title of the book. The value between the <Title> start tag and the </Title> end tag is the title (in this case, Beginning VB 2005.)

As in HTML, XML can also use what are known as attributes. An attribute is a named piece of information descriptive to the node (element) wherein it is located. When you use attributes, you must enclose them in quotes. Here is the same XML fragment as the previous one, but this time using attributes:

<Book>

<Title ISBN=”xxxx574019”>Beginning VB 2005</Title>

<Publisher>Wrox</Publisher>

</Book>

XML is largely common sense, which is one of the things that make it so simple. For example, you can probably guess what this document represents, even though you may have only just started thinking about XML:

<Books>

<Book>

<Title>Beginning VB 2005 </Title> <ISBN>xxxx574019</ISBN> <Publisher>Wrox</Publisher>

</Book>

<Book>

<Title>Professional Visual Basic.Net</Title> <ISBN>1861005555</ISBN> <Publisher>Wrox</Publisher>

</Book>

</Books>

XML for Visual Basic Newcomers

As a newcomer to programming and Visual Basic, it is unlikely that you will be undertaking projects that involve complex integration work. If XML is so popular because it makes systems integration so much easier, how is it relevant to a newcomer?

The answer to this question is that, as well as being a great tool for integration, XML is also a great tool for storage and general data organization. Before XML, the two ways that an application could store its data were by using a separate database or by having its own proprietary file format with code that could save into and read from it.

In many cases, a database is absolutely the right tool for the job, because you need the fast access, shared storage, and advanced searching facilities that a database such as Access or SQL Server gives you. In other cases, such as with a graphics package or word processor, building your own proprietary format is the right way to go. The reasons for this may be you want the application to be light and do not want to have the hassle of showing the user how to set up and maintain a database, or simply do not want to deal with the licensing implications of needing a separate application to support yours.

614

Visual Basic 2005 and XML

XML gives you a new way of storing application data, though it is still based on the concept of defining your own proprietary application storage format. The key difference, in contrast to formats such as .doc files for Word documents, however, is that the XML storage format is a universal standard.

The Address Book Project

You’re going to build a demonstration application that allows you to create an XML file format for an address book. You’ll be able to create a list of new addresses and save the whole lot as an XML file on your local disk. You’ll also be able to load the XML file and walk through the addresses one by one.

Creating the Project

As always, the first thing you have to do is create a new project.

Try It Out

Creating the Project

1.Open Visual Studio 2005 and select File New Project from the menu. Create a new Visual Basic .NET Windows Application project and call it Address Book.

2.The Form Designer for Form1 will open. Change its Text property to Address Book. Now add ten text boxes, twelve labels, and a button to the form so that it looks like Figure 19-1. Notice that you have grid alignment bars to help align the controls on the form. Another option to align controls is the Format menu options.

Figure 19-1

3.The text boxes should be named as follows, in the order given:

1.txtFirstName

2.txtLastName

615