
C# ПІДРУЧНИКИ / c# / Premier Press - C# Professional Projects
.pdf
368 Project 3 CREATING A CREATIVE LEARNING PROJECT
Creating an instance of the Info.ico file adds the file to the Icon class of the System.Drawing namespace. When the notification icon is displayed in the status area, you can use the Text property of the notification icon to display a ToolTip. The Text property appends the word Processed: to the name of the file that is being processed. Figure 17-1 displays the notification icon with a ToolTip.
Y
FIGURE 17-1 Notification icon with a ToolTipLin the status area
Extracting Data from a WordFDocument
the Word application, you need to create an instance of the Word.Application-
M A Your application is nowEready to process the Word document. However, to access
Class class. After you are able to access the Word application, you can create an
instance of the Word.DocumentClass class to access the Word document. To do so,
add the following statements to the |
Created event of the FileSystemWatcher |
|
component. |
T |
|
Word.Application wdApp= new Word.ApplicationClass();
Word.Document Doc = new Word.DocumentClass();
After creating the instance of the Word document Doc, you can use the instance to open the document file that the user adds to the source directory. To do this, you can use the Open() method of the Documents class. The Open() method takes 12 parameters. However, only the first parameter, which is an object containing the file name to be opened, is essential. To pass FileName as a parameter to the Open() method, you can create an object, filename, which stores the name of the file and the full path of the directory where the file is stored. To create the filename object, use the following statement:
object filename=e.FullPath;
TIP
The filename object is passed as a reference parameter to the Open() method.
Team-Fly®

INTERACTING WITH A MICROSOFT WORD DOCUMENT |
Chapter 17 |
369 |
|
|
|
|
|
Except for the first parameter, the rest of the parameters of the Open() method are optional. Therefore, you can create an object of the instance of the Missing class
optional.
NOTE
The Missing class is a public sealed class in the System.Reflection namespace. You cannot inherit a Missing class.
The optional object can now be passed as optional parameters to the Open() method. To do this, use the following statements:
object optional=System.Reflection.Missing.Value;
Doc=wdApp.Documents.Open(ref filename, ref optional, ref optional,ref optional,ref
optional,ref optional,ref optional,ref optional,ref optional,ref optional,ref
optional,ref optional);
The preceding code opens the Word document present in the source directory and stores the entire content of the Word document in the instance of the Word.DocumentClass class Doc. However, in this case, you only require the information in the Cash Memo No. and Total amount payable fields. Figure 17-2 shows a sample cash memo document.
FIGURE 17-2 Sample cash memo document

370 Project 3 CREATING A CREATIVE LEARNING PROJECT
To retrieve the required data from the cash memo document, add the following code to the Created event:
Word.Range wdRange; wdRange=Doc.Paragraphs.Item(2).Range; string strMemo, strAmount;
int intParacount; strMemo=wdRange.Text; strMemo=strMemo.Substring(15,4); intParacount=Doc.Paragraphs.Count; intParacount=intParacount-2;
wdRange=Doc.Paragraphs.Item(intParacount).Range; object count=”-1”;
object wdCharacter=”1”;
wdRange.MoveEnd(ref wdCharacter,ref count); strAmount=wdRange.Text; strAmount=strAmount.Substring(23);
The preceding code creates an object of the type Range, wdRange that stores the content of the Word document. You then use the Item property of the Paragraphs collection to retrieve data from a specified paragraph. As shown in Figure 17-2, Cash Memo No. is the second paragraph in the cash memo document. Therefore, you need to retrieve the content of the second paragraph of the cash memo document by using the Range property. The content that is retrieved is then stored in
wdRange.
The Text property of the wdRange object is used to store the text of the paragraph in a string type variable, strMemo. Until now, the strMemo variable stores the entire content of the second paragraph. However, to just retrieve the value of the Cash Memo No. field, use the Substring() method. The Substring() method takes two parameters, the starting position from where the text is retrieved and the number of characters retrieved.
Similarly, you can store the text of the Total amount payable field in another string type variable, strAmount. The Total amount payable field is the second last paragraph in the cash memo document.Therefore, you need to declare an integer variable, intParacount, that stores the number of paragraphs in a document. You use the Count property of the Paragraphs collection to count the number of paragraphs in the document.

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

INTERACTING WITH A MICROSOFT WORD DOCUMENT |
Chapter 17 |
|
377 |
|
|
||||
|
|
|
|
|
After the application writes the data to the Summary.xml document, the application is ready to process the next document. Therefore, you need to again change the notification icon to Ready.ico. To do this, type the following statement in the
Created event.
icoNotify.Icon=m_Ready;
Figure 17-6 shows the Ready.ico notification icon in the status area of the taskbar.
FIGURE 17-6 Ready.ico notification icon in the status area of the taskbar
However, while processing a document, the application might detect an error, such as incorrect or incomplete data in the cash memo document. In such a case, the application generates an error message in Event Log.
Displaying an Error Message in the Event Log
Event logging is a method by which the Microsoft Windows operating system keeps track of all the important software and hardware events running on the system. Tracking the events helps the system administrator to detect the cause of any error that occurs on the system. You can view these software and hardware events in Event Viewer provided by Microsoft Windows. To access Event Viewer, perform the following steps:
1.Point to the Settings option on the Start menu.
2.In the displayed list, click on the Control Panel option. The Control Panel window is displayed.
3.In the Control Panel window, click on the Administrative Tools option. The Administrative Tools window is displayed.