You are on page 1of 38

Objectives

Work with XML


Work with XML using the DOM Discuss the Service Control Manager

Create a Windows Service program


Create a Service Configuration program

Introduction (1)
XML forms an integral part of computing
Acts as a universal glue Binds together varied formats of database to

connect and exchange data C# applications can utilize and benefit from XML

Introduction (2)
The System.XML namespace handles XML in .NET
XMLReader & XMLWriter classes are used to read and

write XML documents XML Document Object Model implementation in .NET

Introduction (3)
Validating XML documents using the

XMLValidatingReader class Creating XML files from an ADO.NET

record set

System.XML namespace
This namespace contains a number of classes that

help us process XML documents

XmlWriter and XmlReader


The XmlWriter and XmlReader classes are abstract

classes that are required to read and write XML data from streams. These two abstract classes define the common functionality that all the classes derived from them must support

XmlTextReader and XmlTextWriter


To read and write XML documents to a text file, we need to use the XmlTextReader and XmlTextWriter classes. These two classes are derived from the XMLReader and XMLWriter classes

XmlDocument
The XmlDocument class gives a tree representation of

an XML document, which enables its navigation and editing

Writing an XML file (1)


Example 1 -

Writing an XML file (2)


Contd

Output in an XML file


Output -

Cont.
Formatting:

Indicates how the output is formatted. If the Indented option is set, child elements are

indented using the Indentation properties


Indentation:

Gets or sets how many IndentChars to write for

each level in the hierarchy when Formatting is set to Formatting.Indented

Cont.
First we imported System.Xml namespace as this namespace

contain the XmlWriter, XmlTextWriter and most of the other classes that are used in manipulating XML data. The constructor of the XmlTextWriter takes in two parameters; the first being the filename for the XML text file that is to be created and the second is the encoding style. Since we will not be using any type of encoding for our XML file, we have specified null as the second parameter and the full filename including the path as the first parameter The WriteStartDocument() method writes the XML declaration with the version "1.0". This method is inherited from the XmlWriter class

Cont.
The WriteStartElement() method writes the specified start tag.

This method takes in three parameters; the first is the namespace prefix of the element that is to be added, the second being the local name of the element and the third being the URI of the namespace used to associate with the element We use the Flush() method to flush the stream. When this method is called, it flushes the buffer and writes whatever data is left in the buffer to the stream We finally close the stream using the Close() method of the object

Reading an XML file (1)


Example 2 -

Reading an XML file (2)


Output -

XML Validation
A well-formed XML document is the one that meets all the syntactical

requirements defined by the W3C Extensible Markup Language (XML) 1.0 specification. However, a valid XML document is not only wellformed but also conforms to constraints defined by its Document Type Definition (DTD) or schema This XML validation is enforced using the XmlValidatingReader class. The XmlValidatingReader class implements the XmlReader class

Validation Type property


The XMLValidatingReader includes a ValidationType

property which is set to the type of validation that needs to be done for example DTD, Schema, None, Auto

DTD file for XML


Example 3 -

Validating XML data (1)


Example 4 -

Validating XML data (2)


Contd..

Validating XML data (3)


Output -

An error is generated because the DTD specifies the DocType name to be Phone while the root element in the XML file is PhoneBook

Document Object Model


The World Wide Web Consortium has specified the XML DOM

as a standard programming interface for handling XML documents. The XML DOM treats an XML document as a tree comprising of nodes. Every individual data item is treated as a node, any text or sub data items are treated as sub-nodes (child nodes). This DOM is implemented in .NET using the XMLNode class. This is an abstract class that represents a node of an XML document. The XMLNodeList is another class that represents an ordered collection of nodes.

Document Object Model[Cont]


The need for a Document Object Model arises, as it is not always feasible to go through the entire XML document. Using the DOM a particular node can be selected and worked upon. This is achieved using the GetElementsByTagName() method of the XmlDocument class. The first step in doing so is to load the document from disk, then select the nodes that we want to work with and manipulate them

DOM in .NET (1)


Example 4 -

DOM in .NET (2)


Output -

Adding data to an XML document (1)


Example 5 -

Adding data to an XML document (2)


Contd..

Contents of Phone.XML
Output -

Modifying data in an XML document (1)


Example 6 -

Modifying data in an XML document (2)


Contd..

Modifying data in an XML document (3)


Output -

Phone.XML modified
Output -

Writing an ADO.NET dataset to an XML file


Example 7 -

MyProducts.XML
Output -

Creating an ADO.NET dataset from an XML file (1)


Example 8 -

Creating an ADO.NET dataset from an XML file (2)


Output -

You might also like