ado.net session11

24
Slide 1 of 24 Ver. 1.0 Developing Database Applications Using ADO.NET and XML Session 11 In this session, you will learn to: Read, write, validate, and modify XML data by using the XML reader and writer classes Objectives

Upload: niit-care

Post on 26-Jun-2015

640 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Ado.net session11

Slide 1 of 24Ver. 1.0

Developing Database Applications Using ADO.NET and XML

Session 11

In this session, you will learn to:Read, write, validate, and modify XML data by using the XML reader and writer classes

Objectives

Page 2: Ado.net session11

Slide 2 of 24Ver. 1.0

Developing Database Applications Using ADO.NET and XML

Session 11

You can process XML data in your .NET applications using the System.Xml namespace.

The System.Xml namespace contains many classes to write and read XML documents.

Using these classes, you can create an XML document, process an XML document, validate an XML document, and so on.

Processing XML Data

Page 3: Ado.net session11

Slide 3 of 24Ver. 1.0

Developing Database Applications Using ADO.NET and XML

Session 11

The XmlWriter class in the System.Xml namespace provides non-cached, forward-only, and write-only access to XML data.

XmlWriter objects are created using the Create() method.

You can pass an object of XmlWriterSettings class to the Create()method in order to specify the settings or properties, which you want to enable on the XmlWriter object. If an XmlWriterSettings is not passed as a parameter, the default settings are applied.

The following line of code snippet creates an XmlWriter object that creates an XML file called myXmlFile.xml:XmlWriter writer = XmlWriter.Create(“myXmlFile.xml”,settings);

Writing XML Data

Page 4: Ado.net session11

Slide 4 of 24Ver. 1.0

Developing Database Applications Using ADO.NET and XML

Session 11

Just a minute

Fill in the blanks:The __________ namespace in .NET contains XML classes.

Answer:System.Xml

Page 5: Ado.net session11

Slide 5 of 24Ver. 1.0

Developing Database Applications Using ADO.NET and XML

Session 11

An XML file includes elements, attributes and comments.Elements

Attributes

Comments

All these can be created by using various methods of XmlWriter class.

Writing XML Data (Contd.)

Page 6: Ado.net session11

Slide 6 of 24Ver. 1.0

Developing Database Applications Using ADO.NET and XML

Session 11

Elements can be created in two ways:

Writing XML Data (Contd.)

By calling the WriteElementString() method

By calling the WriteStartElement() method

The WriteElementString()method takes two parameters, the name of the element and the value of the element.

writer.WriteElementString(“Name”, “Peter”);

Here, writer is an object of XmlWriter, Name is the name of the element and Peter is the value of the element.

The WriteStartElement() method takes the name of the element as a parameter. The WriteString() method to specify a value for this element. The WriteEndElement() method to end the element tag.

writer.WriteStartElement(“Name”);

writer.WriteString(“Peter”);

writer.WriteEndElement();

Page 7: Ado.net session11

Slide 7 of 24Ver. 1.0

Developing Database Applications Using ADO.NET and XML

Session 11

Just a minute

What is the code snippet to store the value of an integer type variable named Age into an XML file?

Answer://writer is an object of XmlWriterwriter.WriteElementString(“Age”,XmlConvert.ToString(Age));

Page 8: Ado.net session11

Slide 8 of 24Ver. 1.0

Developing Database Applications Using ADO.NET and XML

Session 11

Attributes can be created in two ways:

Writing XML Data (Contd.)

By calling the WriteAttributeString() method

By calling the WriteStartAttribute() method

This method takes the name of the attribute and the value of the attribute as parameters.

//writer is an object of //XmlWriter

writer.WriteAttributeString(“OrderID”,“O001”);

In the preceding code snippet, an attribute OrderID with the value O001 is created.

This method takes the name of the attribute as a parameter. The WriteString() method writes the value of the attribute. The WriteEndAttribute() method ends the attribute tag. writer.WriteStartAttribute("OrderID");

writer.WriteString("O001");

writer.WriteEndAttribute();

Page 9: Ado.net session11

Slide 9 of 24Ver. 1.0

Developing Database Applications Using ADO.NET and XML

Session 11

Comments can be created in the following way:

Writing XML Data (Contd.)

By calling the WriteComment() method

This method takes a string as a parameter. This string is written as a comment in the XML file. The following code snippet writes a comment into an XML file:

//writer is an object of XmlWriter

writer.WriteComment(“This XML file stores product details”);

The text This XML file stores product details will be written within the <!--and --> tags in the XML file.

Page 10: Ado.net session11

Slide 10 of 24Ver. 1.0

Developing Database Applications Using ADO.NET and XML

Session 11

Just a minute

Fill in the blanks:The __________ method of the XmlWriter class is used to write comments into an XML file.

Answer:WriteComment()

Page 11: Ado.net session11

Slide 11 of 24Ver. 1.0

Developing Database Applications Using ADO.NET and XML

Session 11

An XML file can be created in the following ways:Using XmlWriter

Using XmlTextWriter

Saving a Dataset as XML data

Writing XML Data (Contd.)

Page 12: Ado.net session11

Slide 12 of 24Ver. 1.0

Developing Database Applications Using ADO.NET and XML

Session 11

Just a minute

Fill in the blanks:The _______ option of XmlWriteMode write the contents of the dataset as XML data with the dataset structure as an inline schema.

Answer:WriteSchema

Page 13: Ado.net session11

Slide 13 of 24Ver. 1.0

Developing Database Applications Using ADO.NET and XML

Session 11

The XmlReader class in the System.Xml namespace provides non-cached, forward-only, and read-only access to XML data.

XmlReader objects are created using the Create() method.

You can pass an object of XmlReaderSettings class to the Create()method in order to specify the settings or properties, which you want to enable on the XmlReader object. If an XmlReaderSettings is not passed as a parameter, the default settings are applied.

The following code snippet creates an XmlReader object, which reads an XML file called books.xml:

XmlReader reader = null;reader = XmlReader.Create("C:\\books.xml", settings);

Reading XML Data

Page 14: Ado.net session11

Slide 14 of 24Ver. 1.0

Developing Database Applications Using ADO.NET and XML

Session 11

An XML file can be read by using the following classes:XmlReader

XmlTextReader

Reading XML Data (Contd.)

Page 15: Ado.net session11

Slide 15 of 24Ver. 1.0

Developing Database Applications Using ADO.NET and XML

Session 11

Just a minute

Fill in the blanks:The _______ method of the XmlTextReader class reads the content of an element or text node in a string.

Answer:ReadString()

Page 16: Ado.net session11

Slide 16 of 24Ver. 1.0

Developing Database Applications Using ADO.NET and XML

Session 11

An XML file can be validated against a schema using the following two classes:

XmlReader

XmlValidatingReader

Validating XML Data

Page 17: Ado.net session11

Slide 17 of 24Ver. 1.0

Developing Database Applications Using ADO.NET and XML

Session 11

The following code snippet validates an XML file against a schema using XmlReader:

Validating XML Data (Contd.)

XmlSchemaSet schemaSet = new XmlSchemaSet();

schemaSet.Add("urn:product-schema",C:\\products.xsd");

XmlReaderSettings settings = new XmlReaderSettings();settings.ValidationType = ValidationType.Schema;settings.Schemas = schemaSet;

settings.ValidationEventHandler+= new alidationEventHandler (ValidationCallBack);

Create the XmlSchemaSet class.

Add the schema to the collection.

Set the validation settings.

Associate the

ValidationEventHandler to detect validation errors.

Page 18: Ado.net session11

Slide 18 of 24Ver. 1.0

Developing Database Applications Using ADO.NET and XML

Session 11

Validating XML Data (Contd.)

XmlReader reader =XmlReader.Create("C:\\products.xml“,settings);

while (reader.Read());Console.ReadLine();

Create the XmlReader object.

Parse the file

Page 19: Ado.net session11

Slide 19 of 24Ver. 1.0

Developing Database Applications Using ADO.NET and XML

Session 11

The following code snippet validates an XML file against a schema using XmlValidatingReader:

Validating XML Data (Contd.)

XmlTextReader reader = newXmlTextReader("C:\\products.xml");

XmlValidatingReadervalidatingReader = newXmlValidatingReader(reader);

validatingReader.ValidationType= ValidationType.Schema;

Read the products.xml file.

Add validation support the

XmlTextReader object.

Set validation type as Schema.

Page 20: Ado.net session11

Slide 20 of 24Ver. 1.0

Developing Database Applications Using ADO.NET and XML

Session 11

DiffGram:Is an XML format that identifies current and original versions of data elements.

Preserves information about changes to the data in the dataset so that you can choose whether to accept or reject the changes when you read the XML data back to the dataset.

Is created in the following way:

Modifying XML Data Using DiffGrams

ds.WriteXml("C:\\Employee

Details.xml",

XmlWriteMode.DiffGram);

DataSet object

Name of the XML file

Page 21: Ado.net session11

Slide 21 of 24Ver. 1.0

Developing Database Applications Using ADO.NET and XML

Session 11

Problem Statement:Tebisco has its central office in New York and several branch offices across the United States. Tebisco collaborates with various recruitment agencies for hiring purposes. Currently, only the HR management team at the central office is provided with the details of the recruitment agencies. In addition, only the central office interacts with these recruitment agencies. If the branch offices need to interact with the recruitment agencies, they first have to interact with the central office. However, this process is taking time and, therefore, it has been decided to send the details of the recruitment agencies to the branch offices in a suitable format.

Demo: Manipulating XML Data

Page 22: Ado.net session11

Slide 22 of 24Ver. 1.0

Developing Database Applications Using ADO.NET and XML

Session 11

You are a developer in Tebisco. You have been asked to create an application that will retrieve the records stored in the RecruitmentAgencies table into a dataset and perform the following tasks:

1. Save the dataset as XML.

2. Save the dataset as a DiffGram.

Demo: Manipulating XML Data (Contd.)

Page 23: Ado.net session11

Slide 23 of 24Ver. 1.0

Developing Database Applications Using ADO.NET and XML

Session 11

In this session, you learned that:System.Xml namespace contains XML classes. This namespace contains many classes to read and write XML documents.

The XmlWriter class in the System.Xml namespace provides non-cached, forward-only, and write-only access to XML data. This class can be used to write either a stream of data or a text data.

The XmlReader class in the System.Xml namespace provides non-cached, forward-only, and read-only access to XML data. If the XML file is not well formed, the XmlReader raises an XmlException.

Summary

Page 24: Ado.net session11

Slide 24 of 24Ver. 1.0

Developing Database Applications Using ADO.NET and XML

Session 11

An XML document can be validated against a schema by using the following two classes:

XmlReader

XmlValidatingReader

A DiffGram is an XML format that identifies current and original versions of data elements.

Summary (Contd.)