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

Microsoft Visual C++ .NET Professional Projects - Premier Press

.pdf
Скачиваний:
180
Добавлен:
24.05.2014
Размер:
26 Мб
Скачать
☆

Project 9 Overview

XML is talked about nowadays primarily for its use in data exchange amongst diverse applications and platforms. However, one of the earliest uses that was

foreseen for XML was its ability to convert data from one markup format to

 

 

 

 

 

Y

another using XSL. In this project, you will see an example of using .NET’s

XML classes to parse, store, and retrieve XML data.

The concepts that you will use in this project include the following:

 

 

 

 

M

 

 

XML

 

 

L

 

 

 

A

 

 

XSL

 

 

F

 

 

E

 

.NET XML classes

 

 

 

 

 

T

 

 

In this project, you will see how data stored in an SQL server database can be converted into XML. You will also see how, using the classes under the System::Xml namespace, you can parse the XML document and convert it into other markup formats such as HTML.

Team-Fly®

Chapter 28

XML with VC++.NET

You already have seen the importance of XML in the .NET platform. This chapter describes some of the support that .NET provides for XML manipulation. In this chapter, you will create a simple application that a customer-sup- port executive will use to get a list of support calls he/she is supposed to attend. The application will retrieve the calls marked for a particular customer support executive from the database and display it in a GUI application. Before getting into programming the application, I will first introduce the scenario for which you

will design the project.

New-Tech, Inc. – The Scenario

New-Tech, Inc. is a sales organization that sells computer-related products. It also provides maintenance and technical support services. Customers can register with New-Tech for a fee. A registered user can log in to New-Tech’s site and enter a technical query or problem that needs to be solved. Alternatively, a user can place a phone call to New-Tech, in which case one of the customer-support executives, who are on call around the clock, will enter the query in a database. The queries will be sorted, and a technical-support executive will be assigned to each query depending on the location of the customer, the nature of the query, and the category of the query. Each day, the tech-support executives need to check the database and access a list of queries that has been assigned to them. Each technical-support executive’s daily schedule also needs to be published as a report for access on the intranet.

Since accessing the database and publishing the data in HTML format is a cumbersome task for a busy tech-support executive, the project manager of the services team of the company decided to automate the process by creating an application that displays the query list for each tech-support executive and creates the report in HTML format.

XML WITH VC++.NET Chapter 28 813

Project Life Cycle

The development life cycle of a project usually involves three phases:

Project initiation

Project execution

Project deployment

In the project-initiation phase, the project plan was prepared and the development team for the project was identified. The development team further prepared a comprehensive list of tasks involved in the execution and the deployment phases of the project’s life cycle.

Moving further, you will look at the tasks performed by the development team in the following stages of the project-execution phase:

Requirements analysis

Design

Construction

Testing

In the subsequent sections, I will detail the tasks performed by the team in each of these stages.

Requirements Analysis and Design

During the requirement analysis phase, the development team at New-Tech analyzed the need and the use of the Scheduler application and came up with the following requirements:

The application should have a GUI where each tech-support executive’s daily list of queries can be displayed.

The application should allow the executive to convert the data into an HTML-based report.

While drafting the design for the application, the functionality to be implemented and the technology to be used were also determined. It was decided that the data retrieved from the database should be stored in XML format so that it can be displayed in the GUI and, if required, easily be converted into HTML. Also, the team foresees that the tech-support executives not only will need to access the data through the application, but once they have left for the support calls, might

814 Project 9 XML WITH VC++.NET

also need to access it when on the move. Therefore, the team decided to design the report in HTML format. The HTML report can also be published on the company’s Web site, thereby enabling access to the report from anywhere.

In addition, there might be a need in the future to publish the same report in WML format, thus facilitating access to the report directly through mobile phone, without the need for a computer and an Internet connection.

Keeping these requirements in mind, the team decided that the data should be stored in XML format and be converted into HTML using XSL. This way, if the document needs to be accessible in some other format, like WML, all it will need is a new XSL file for that format.

Design of the Application

The application will be built as a Managed C++ application. The main form of the application will have the UI design shown in Figure 28-1.

FIGURE 28-1 The design of the Scheduler application

When the application is run, the user can retrieve the queries assigned to him or her by entering his or her sign-in name (as it would be in the database) and clicking on the Get Details button. This should retrieve the data from the database and populate the ListView control. Once the data has been retrieved, the Generate

XML WITH VC++.NET Chapter 28 815

Report button will be enabled. Clicking on it will generate the listing in HTML format.

Figure 28-2 displays the structure of the table where all customer queries will be stored.

FIGURE 28-2 Design of the table containing Customer queries

Construction

During this phase, the project development team at New-Tech will construct the application. The following are the primary tasks that the team needs to perform:

Create a Managed C++ application that reads and writes XML data.

Create the XSL for the XML-to-HTML conversion.

XML in .NET–The .NET XML Classes

The entire .NET platform is based on the XML standard; hence, the way you program to use XML in your own applications also has been changed and improved. With earlier versions of Visual Studio, you would use the XML API of

816 Project 9 XML WITH VC++.NET

the MSXML parser to access the Document Object Model (DOM) of an XML document and manipulate it using the DOM. However, with .NET, there are a number of new classes that make accessing XML documents a lot easier and faster. XML is at the core of the .NET platform. Most parts of the .NET Framework, such as ASP.NET, Web services, and so on, use XML as their preferred data representation format.

The .NET Framework’s XML classes are also very tightly coupled with ADO.NET. Even though ADO did offer XML support, it was very minimal. ADO.NET has a dataset object that allows you to retrieve and store data in XML format. You can query a DataSet object as XML, traverse it using its DOM, query it using XPath, and transform it using XSLT.

Of course, you always have the option of using the old MSXML parser API model through the COM .NET interop feature. However, you will hardly need to do that since the DOM Level 2 API is still accessible through .NET classes. Also, the .NET Framework XML classes introduce an innovative stream-based API.

The .NET Framework XML classes are split up into several namespaces. The core types can be found in the System.XML namespace. The types that are specific to either XPath or XSLT are in the System.Xml.Xpath namespace. All of these classes are packaged in a single assembly, System.Xml.dll. As a Managed C++ programmer, you will need to reference this file in your application. To use the XML classes, include the following declarations in your application:

#using <System.Xml.dll>;

using namespace System.Xml;

using namespace System.Xml.Xpath;

At the base of the XML classes’ hierarchy are the following abstract classes:

XmlReader

XmlWriter

The XmlReader class provides an interface for forward-only, read-only access of an XML document. The XmlWriter class provides an interface for creating XML document streams that are standards-compliant. Applications that need to access XML documents will use the XmlReader class or its derivatives, while applications that need to create XML documents will use the XmlWriter class or its derivatives. Both the classes, XmlReader and XmlWriter, support a model that does not load

XML WITH VC++.NET Chapter 28 817

the XML document into the memory, but rather streams it for access (somewhat similar to SAX API) that makes them faster and lighter.

The XmlReader class has XmlTextReader, XmlNodeReader, and XvalidatingReader

classes derived from it. The XmlWriter class has XmlTextWriter and XmlNodeWriter classes derived from it.

Take a quick glance at each of these classes.

XmlTextReader Class

This class is an abstract base class that provides forward-only, read-only access to an XML document. This class has the following features:

Provides fast access to XML document content.

Validates the XML document for well formedness.

Validates the document and the accompanying DTD (if any) for well formedness. However the document is not checked for compliance with the DTD or a schema.

XmlValidatingReader Class

This class is similar to the XmlTextReader class except that it can validate the XML document with the accompanying DTD or schema.

XmlReader Class

This class provides fast access to XML content within a node of an XML document.

As mentioned earlier, the XmlReader class provides forward-only access to the XML document. This it does by allowing you to jump from the current node to the next. Hence, most of the properties and methods of the class are applicable to the current node loaded.

The following are some of the important properties and methods of the XmlReader class, and thus are a part of its derived classes, too:

Name. This property gives the name of the current node in the reader. For example, consider the following XML node:

818 Project 9 XML WITH VC++.NET

<category id=”IT”>

<category_name>Information Technology</category_name>

<title>Advantages of CRM</title>

</category>

Assume that the document containing the node listed in the preceding code has been loaded into an XmlReader object:

XmlTextReader *reader= new XmlTextReader(“abc.xml”);

Reader->Reader();

Reader->Name;

The Reader->Name property will return the string category name.

NodeType. This property gives you the type of the node that is currently loaded in the reader object. The following are some of the valid

node types: Element, Entity, Notation, ProcessingInstruction, Text,

WhiteSpace, and XmlDeclaration.

Value. This property will return the text value of the current node. For example, in the preceding XML document, if the current node is <cate-

gory_name>, a call on the Read() method will take you to the next node. Here this property would return ‘Information Technology’.

Read. This method when overridden in a derived class, such as XmlTextReader, will read the next node from the input stream. It is used to read an XML document completely. An example illustrating the use of the Read method to read the contents of an XML file follows:

XmlTextReader *reader= new XmlTextReader(“abc.xml”);

while(reader->Read())

{

}

MoveToAttribute. This method is used to move to the first attribute of the current element type node. For example, if the following XML document is loaded and the current node is <category>, calling MoveTo will move the reader to the id attribute node:

<category id=”IT”>

<category_name>Information Technology</category_name>

<title>Advantages of CRM</title>

</category>

XML WITH VC++.NET Chapter 28 819

MoveToElement. Once you have moved to an attribute node using the MoveToAttribute method, you can return to the element node using this method.

MoveToFirstAttribute. Moves to the first attribute node of the current element node.

MoveToNextAttribute. Moves to the next attribute node.

ReadAttributeValue. Gets the value of the current attribute.

NOTE

A typical XML document node such as <title>Advantages of CRM</title> is actually a set of three nodes for the XmlReader. The <title> element is a node of type Element. The </title> element is a node of type EndElement. The text in between the title tags is a node of type Text.

XmlWriter Class

Similar to the XmlReader class, the XmlWriter class is also an abstract base class that defines the functionality to create XML documents that conform to the W3C standards. There are two important classes that implement XmlWriter:

XmlTextWriter

XmlNodeWriter

The XmlWriter class makes it easy to write out all standard XML constructs (such as elements, attributes, and processing instructions) using the corresponding methods. The following is an example that illustrates the same:

writer->WriteStartDocument();

writer->WriteComment(“This Is just a sample XML file”);

writer->WriteStartElement(“Article”);

writer->WriteStartElement(“Title”);

writer->WriteString(“The advantage of e-CRM”);

writer->WriteEndElement();

writer->WriteStartElement(“Author”);

writer->WriteString(“John Doe”);

writer->WriteEndElement();

writer->WriteEndElement();

writer->WriteEndDocument();