Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Microsoft C# Professional Projects - Premier Press.pdf
Скачиваний:
177
Добавлен:
24.05.2014
Размер:
14.65 Mб
Скачать

INTERACTING WITH A MICROSOFT WORD DOCUMENT

Chapter 17

 

371

 

 

 

 

 

 

You can store the data that you have retrieved from the cash memo document in an XML document. However, before storing the data in an XML document, you need to understand the basics of XML.

Overview of XML

XML (Extensible Markup Language) is a standard defined for W3C (World Wide Web Consortium) that you can use to store and display data in a structured format. The data in an XML document is displayed as plain text to provide you with a standard interface to display data across multiple platforms. Because of this, XML is extensively used to create applications that run on the Internet. To display data in an XML document, you use tags. You can now write a simple XML code that displays data in the Internet Explorer window. To create an XML document, type the following code in a Notepad file.

<?xml version=”1.0”?> <Students>

<Student>

< StudentId> St001 </StudentId> <LastName> Brown </LastName> <FirstName> George </FirstName>

</Student> <Student>

< StudentId> St002 </StudentId> <LastName> Floyd </LastName> <FirstName> Nancy </FirstName>

</Student> <Student>

< StudentId> St003 </StudentId> <LastName> Smith </LastName> <FirstName> James </FirstName>

</Student> </Students>

In the preceding code, the first line <?xml version= “1.0”?> is an XML declaration statement that is used to indicate to the browser that the document being processed is an XML document. The tag Student is used to denote an element

that contains StudentId, LastName, and FirstName as nodes.

372 Project 3 CREATING A CREATIVE LEARNING PROJECT

To view the output of the preceding code, save the Notepad file as Student.xml and open the file in Internet Explorer. Figure 17-3 shows the Student.xml file in Internet Explorer.

FIGURE 17-3 The Student.xml file in Internet Explorer

Visual Studio .NET provides you with several classes and APIs (application programming interfaces) that you can use to display and process data in an XML document.The following section discusses a few classes that you use to read and write data from an XML document.

The XmlReader Class

The XmlReader class is an abstract base class in the Visual Studio .NET base class library that allows you to access XML data. The XmlReader class lies in the System.Xml namespace and provides you with the ability to create a customized reader. In addition, the XmlReader class allows you to implement the functionality

of derived classes such as XmlTextReader, XmlValidatingReader, and Xml-

NodeReader. The XmlReader class provides several methods and properties to access data from XML documents.

INTERACTING WITH A MICROSOFT WORD DOCUMENT

Chapter 17

373

 

 

 

 

The XmlWriter Class

The XmlWriter class is another abstract base class in the System.Xml namespace that allows you to write data to an XML document. You can use the XmlWriter class to create an interface for the XML documents. These XML documents are created using streams generated by the XmlWriter class and conform to the W3C and Namespace recommendations. The XmlWriter class implements the XmlTextWriter class. The XmlWriter class contains several methods and classes that you can use to write data to streams.

Displaying Data in an XML Document

Before writing the code to display data in an XML document, you should first see a sample XML document created by the Creative Learning application. Figure 17-4 shows the sample XML document.

FIGURE 17-4 Sample XML document

To display data in an XML document, you first need to create an object, xmlWrite, of the XmlTextWriter class in the System.Xml.XmlWriter class. After creating the xmlWrite object, you can use this object to write data to the XML document. The following section discusses how to add code to the XML document.

374 Project 3 CREATING A CREATIVE LEARNING PROJECT

Adding Code to the XML Document

To write data to the XML document, add the following code to the Created event.

XmlTextWriter xmlWrite; xmlWrite.Formatting=Formatting.Indented; xmlWrite.WriteDocType(“Sales”,null,null,null); xmlWrite.WriteComment(“Summary of sales at Creative Learning”); xmlWrite.WriteStartElement(“Sales”); xmlWrite.WriteStartElement(Convert.ToString(DateTime.Today)); xmlWrite.WriteElementString(“Memo”,strMemo); xmlWrite.WriteElementString(“Amount”,strAmount); xmlWrite.WriteEndElement();

xmlWrite.WriteEndElement();

The preceding code creates an object, xmlWrite, of the XmlTextWriter class.Then, the code uses the WriteDocType() method of the XmlTextWriter class to write the DOCTYPE declaration of the XML document. The WriteDocType() method takes four parameters.The first parameter specifies the name of the DOCTYPE, Sales, and is an essential parameter. However, the other three parameters are optional.

The DOCTYPE Declaration

The DOCTYPE declaration is used to specify DTD (Document Type Definition) for an XML document. DTD contains a set of rules that you can use to define the structure and logic of XML documents. You can create a document called a DTD document to store the set of rules for a specific XML document. The DTD documents contain rules that conform to W3C standards and are used to define the structure and syntax of the XML documents. In addition, the DTD documents contain the content and values allowed for an XML document as per W3C standards. The DTD document is a file with the extension .dtd.

When you create an XML document, the document is validated against the rules specified in the DTD document. To do this, you need to associate an XML document with a DTD document by using the DOCTYPE declaration. In addition to the name of the document, you can specify the root element of the document.

After discussing the DOCTYPE declaration statement, I will continue with the discussion of the code that is used to write data to the XML document. In addition to specifying a DOCTYPE for your XML document, you can add a com-

INTERACTING WITH A MICROSOFT WORD DOCUMENT

Chapter 17

 

375

 

 

 

 

 

 

ment to the XML document. To add a comment, you can use the WriteComment() method of the XmlTextWriter class. The WriteComment() method takes the text to be displayed in the form of a comment as a parameter.

After specifying the DOCTYPE and a comment to the XML document, you need to add data to the document.The data in an XML document is displayed in the form of elements and nodes. Each element that you specify is enclosed within tags. To specify the starting tag for an element, you use the WriteStartElement() method. The WriteStartElement() method is a void method in the XmlTextWriter class and is used to specify the starting tag for an element and associate the element with the given namespace. The WriteStartElement() method takes the name of the element as a parameter.

You can use the WriteStartElement() method to start two elements, Sales and current date. To specify the current date, use the DateTime struct in the System namespace. The Today property of the DateTime struct is used to retrieve the current date. The value returned by the Today property of the DateTime struct is converted to a string type value by using the ToString() method of the Convert class.

NOTE

The ToString() method is used to convert a 64-bit signed integer to its equivalent string type value represented by the System.String class in the specified base. The ToString() method is a method in the Convert class that lies in the System namespace.

Once you have added elements to your XML document, you can add nodes to the elements. To add nodes to the current date element, use the WriteElementString() method XmlWrite class. When you override the WriteElementString() method in a derived class, you can use it to create an element with the parameters that you specify. The WriteElementString() method takes the name of the element and its value as the parameter. You can use the WriteElementString() method to specify Memo and Amount as nodes in the current date element.

After adding data to the element, you need to close the element tag. You can do

this by using the WriteEndElement() method. The WriteEndElement() method is a void method in the XmlTextWriter class.

376 Project 3 CREATING A CREATIVE LEARNING PROJECT

TIP

You need to call the WriteEndElement() method of the XmlTextWriter class as many times as the number of elements in your XML document.This means that calling the WriteEndElement() method once will not close all the elements in the XML document.

You have created an object of the XmlTextWriter class xmlWrite. You have also used it to write the data to an XML document. However, until now, you have not specified the name of the XML document and the directory where the XML document needs to be stored. To do this, add the following statement to the Created event.The name of the file is specified as Summary.xml, and the destination directory is the directory specified in the txtDest text box.

xmlWrite= new XmlTextWriter(txtDest.Text + “Summary.xml”,null);

You can also use the Intended enum of the System.Xml.Formatting enum to format the data that is displayed in the XML document. The System.Xml.Formatting enum is used to specify the format settings for the objects of the System.Xml.XmlTextWriter class. Using the Intended enum enables you to display the data as per the

System.Xml.XmlTextWriter.Indentation and System.Xml.XmlTextWriter.IndentChar

settings. Figure 17-5 shows the XML document before you format the data in the document.

FIGURE 17-5 Data in the XML document without formatting