1. Developing Database Applications Using ADO.NET and XML
Objectives
In this session, you will learn to:
Read, write, validate, and modify XML data by using the XML
reader and writer classes
Ver. 1.0 Session 11 Slide 1 of 24
2. Developing Database Applications Using ADO.NET and XML
Processing XML Data
• 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.
Ver. 1.0 Session 11 Slide 2 of 24
3. Developing Database Applications Using ADO.NET and XML
Writing XML Data
• 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);
Ver. 1.0 Session 11 Slide 3 of 24
4. Developing Database Applications Using ADO.NET and XML
Just a minute
Fill in the blanks:
The __________ namespace in .NET contains XML classes.
Answer:
System.Xml
Ver. 1.0 Session 11 Slide 4 of 24
5. Developing Database Applications Using ADO.NET and XML
Writing XML Data (Contd.)
An XML file includes elements, attributes and comments.
Elements
Attributes
Comments
• All these can be created by using various methods of
XmlWriter class.
Ver. 1.0 Session 11 Slide 5 of 24
6. Developing Database Applications Using ADO.NET and XML
Writing XML Data (Contd.)
Elements can be created in two ways:
By calling the The WriteElementString()method takes
WriteElementString() two value of the element. of the element and
the
parameters, the name
method 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.
By calling the The WriteStartElement() method takes
the name of the element as a parameter. The
WriteStartElement() WriteString() method to specify a value for
method this element. The WriteEndElement()
method to end the element tag.
writer.WriteStartElement(“Name”);
writer.WriteString(“Peter”);
writer.WriteEndElement();
Ver. 1.0 Session 11 Slide 6 of 24
7. Developing Database Applications Using ADO.NET and XML
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 XmlWriter
writer.WriteElementString(“Age”,XmlConvert.To
String(Age));
Ver. 1.0 Session 11 Slide 7 of 24
8. Developing Database Applications Using ADO.NET and XML
Writing XML Data (Contd.)
Attributes can be created in two ways:
By calling the This method takes the name of the
attribute and the value of the attribute as
WriteAttributeString() parameters.
method //writer is an object of
//XmlWriter
writer.WriteAttributeString(“O
rderID”,“O001”);
In the preceding code snippet, an
attribute OrderID with the value
By calling the O001 is created.
WriteStartAttribute() This method takes the name of the
method attribute as a parameter. The
WriteString() method writes the
value of the attribute. The
WriteEndAttribute() method ends
the attribute tag.
writer.WriteStartAttribute("Or
derID");
writer.WriteString("O001");
Ver. 1.0 Session 11 writer.WriteEndAttribute();8 of 24
Slide
9. Developing Database Applications Using ADO.NET and XML
Writing XML Data (Contd.)
Comments can be created in the following way:
By calling the This method takes a string as a
parameter. This string is written as a
WriteComment() comment in the XML file. The following
method 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.
Ver. 1.0 Session 11 Slide 9 of 24
10. Developing Database Applications Using ADO.NET and XML
Just a minute
Fill in the blanks:
The __________ method of the XmlWriter class is used to
write comments into an XML file.
Answer:
WriteComment()
Ver. 1.0 Session 11 Slide 10 of 24
11. Developing Database Applications Using ADO.NET and XML
Writing XML Data (Contd.)
An XML file can be created in the following ways:
Using XmlWriter
Using XmlTextWriter
Saving a Dataset as XML data
Ver. 1.0 Session 11 Slide 11 of 24
12. Developing Database Applications Using ADO.NET and XML
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
Ver. 1.0 Session 11 Slide 12 of 24
13. Developing Database Applications Using ADO.NET and XML
Reading XML Data
• 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);
Ver. 1.0 Session 11 Slide 13 of 24
14. Developing Database Applications Using ADO.NET and XML
Reading XML Data (Contd.)
An XML file can be read by using the following classes:
XmlReader
XmlTextReader
Ver. 1.0 Session 11 Slide 14 of 24
15. Developing Database Applications Using ADO.NET and XML
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()
Ver. 1.0 Session 11 Slide 15 of 24
16. Developing Database Applications Using ADO.NET and XML
Validating XML Data
An XML file can be validated against a schema using the
following two classes:
XmlReader
XmlValidatingReader
Ver. 1.0 Session 11 Slide 16 of 24
17. Developing Database Applications Using ADO.NET and XML
Validating XML Data (Contd.)
• The following code snippet validates an XML file against a
schema using XmlReader:
XmlSchemaSet schemaSet = new Create the XmlSchemaSet
XmlSchemaSet(); class.
schemaSet.Add("urn:product Add the schema to the
collection.
-schema",C:products.xsd");
Set the validation settings.
XmlReaderSettings settings =
new XmlReaderSettings();
settings.ValidationType =
ValidationType.Schema;
settings.Schemas = schemaSet; Associate the
ValidationEventHandler
settings.ValidationEventHandler
to detect validation errors.
+= new alidationEventHandler
(ValidationCallBack);
Ver. 1.0 Session 11 Slide 17 of 24
18. Developing Database Applications Using ADO.NET and XML
Validating XML Data (Contd.)
XmlReader reader = Create the XmlReader
XmlReader.Create object.
("C:products.xml“,settings);
while (reader.Read());
Parse the file
Console.ReadLine();
Ver. 1.0 Session 11 Slide 18 of 24
19. Developing Database Applications Using ADO.NET and XML
Validating XML Data (Contd.)
• The following code snippet validates an XML file against a
schema using XmlValidatingReader:
XmlTextReader reader = new Read the products.xml file.
XmlTextReader
("C:products.xml");
XmlValidatingReader Add validation support the
validatingReader = new XmlTextReader object.
XmlValidatingReader(reader);
validatingReader.ValidationType Set validation type as Schema.
= ValidationType.Schema;
Ver. 1.0 Session 11 Slide 19 of 24
20. Developing Database Applications Using ADO.NET and XML
Modifying XML Data Using DiffGrams
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:
ds.WriteXml("C:Employee DataSet object
Details.xml", Name of the XML file
XmlWriteMode.DiffGram);
Ver. 1.0 Session 11 Slide 20 of 24
21. Developing Database Applications Using ADO.NET and XML
Demo: Manipulating XML Data
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.
Ver. 1.0 Session 11 Slide 21 of 24
22. Developing Database Applications Using ADO.NET and XML
Demo: Manipulating XML Data (Contd.)
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.
Ver. 1.0 Session 11 Slide 22 of 24
23. Developing Database Applications Using ADO.NET and XML
Summary
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.
Ver. 1.0 Session 11 Slide 23 of 24
24. Developing Database Applications Using ADO.NET and XML
Summary (Contd.)
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.
Ver. 1.0 Session 11 Slide 24 of 24
Editor's Notes
Introduce the students to the course by asking them what they know about forensics. Next, ask the students what they know about system forensics and why is it required in organizations dependent on IT. This could be a brief discussion of about 5 minutes. Lead the discussion to the objectives of this chapter.
Introduce the students to the course by asking them what they know about forensics. Next, ask the students what they know about system forensics and why is it required in organizations dependent on IT. This could be a brief discussion of about 5 minutes. Lead the discussion to the objectives of this chapter.
Introduce the students to the course by asking them what they know about forensics. Next, ask the students what they know about system forensics and why is it required in organizations dependent on IT. This could be a brief discussion of about 5 minutes. Lead the discussion to the objectives of this chapter.
Introduce the students to the course by asking them what they know about forensics. Next, ask the students what they know about system forensics and why is it required in organizations dependent on IT. This could be a brief discussion of about 5 minutes. Lead the discussion to the objectives of this chapter.
Introduce the students to the course by asking them what they know about forensics. Next, ask the students what they know about system forensics and why is it required in organizations dependent on IT. This could be a brief discussion of about 5 minutes. Lead the discussion to the objectives of this chapter.
Introduce the students to the course by asking them what they know about forensics. Next, ask the students what they know about system forensics and why is it required in organizations dependent on IT. This could be a brief discussion of about 5 minutes. Lead the discussion to the objectives of this chapter.
Introduce the students to the course by asking them what they know about forensics. Next, ask the students what they know about system forensics and why is it required in organizations dependent on IT. This could be a brief discussion of about 5 minutes. Lead the discussion to the objectives of this chapter.
Introduce the students to the course by asking them what they know about forensics. Next, ask the students what they know about system forensics and why is it required in organizations dependent on IT. This could be a brief discussion of about 5 minutes. Lead the discussion to the objectives of this chapter.
Introduce the students to the course by asking them what they know about forensics. Next, ask the students what they know about system forensics and why is it required in organizations dependent on IT. This could be a brief discussion of about 5 minutes. Lead the discussion to the objectives of this chapter.
Introduce the students to the course by asking them what they know about forensics. Next, ask the students what they know about system forensics and why is it required in organizations dependent on IT. This could be a brief discussion of about 5 minutes. Lead the discussion to the objectives of this chapter.
Introduce the students to the course by asking them what they know about forensics. Next, ask the students what they know about system forensics and why is it required in organizations dependent on IT. This could be a brief discussion of about 5 minutes. Lead the discussion to the objectives of this chapter.
Introduce the students to the course by asking them what they know about forensics. Next, ask the students what they know about system forensics and why is it required in organizations dependent on IT. This could be a brief discussion of about 5 minutes. Lead the discussion to the objectives of this chapter.
Introduce the students to the course by asking them what they know about forensics. Next, ask the students what they know about system forensics and why is it required in organizations dependent on IT. This could be a brief discussion of about 5 minutes. Lead the discussion to the objectives of this chapter.
Introduce the students to the course by asking them what they know about forensics. Next, ask the students what they know about system forensics and why is it required in organizations dependent on IT. This could be a brief discussion of about 5 minutes. Lead the discussion to the objectives of this chapter.
Introduce the students to the course by asking them what they know about forensics. Next, ask the students what they know about system forensics and why is it required in organizations dependent on IT. This could be a brief discussion of about 5 minutes. Lead the discussion to the objectives of this chapter.
Introduce the students to the course by asking them what they know about forensics. Next, ask the students what they know about system forensics and why is it required in organizations dependent on IT. This could be a brief discussion of about 5 minutes. Lead the discussion to the objectives of this chapter.
Introduce the students to the course by asking them what they know about forensics. Next, ask the students what they know about system forensics and why is it required in organizations dependent on IT. This could be a brief discussion of about 5 minutes. Lead the discussion to the objectives of this chapter.
Introduce the students to the course by asking them what they know about forensics. Next, ask the students what they know about system forensics and why is it required in organizations dependent on IT. This could be a brief discussion of about 5 minutes. Lead the discussion to the objectives of this chapter.
Introduce the students to the course by asking them what they know about forensics. Next, ask the students what they know about system forensics and why is it required in organizations dependent on IT. This could be a brief discussion of about 5 minutes. Lead the discussion to the objectives of this chapter.
Introduce the students to the course by asking them what they know about forensics. Next, ask the students what they know about system forensics and why is it required in organizations dependent on IT. This could be a brief discussion of about 5 minutes. Lead the discussion to the objectives of this chapter.
Introduce the students to the course by asking them what they know about forensics. Next, ask the students what they know about system forensics and why is it required in organizations dependent on IT. This could be a brief discussion of about 5 minutes. Lead the discussion to the objectives of this chapter.
Introduce the students to the course by asking them what they know about forensics. Next, ask the students what they know about system forensics and why is it required in organizations dependent on IT. This could be a brief discussion of about 5 minutes. Lead the discussion to the objectives of this chapter.
Introduce the students to the course by asking them what they know about forensics. Next, ask the students what they know about system forensics and why is it required in organizations dependent on IT. This could be a brief discussion of about 5 minutes. Lead the discussion to the objectives of this chapter.
Introduce the students to the course by asking them what they know about forensics. Next, ask the students what they know about system forensics and why is it required in organizations dependent on IT. This could be a brief discussion of about 5 minutes. Lead the discussion to the objectives of this chapter.