• No se han encontrado resultados

2.   EVALUACIÓN DE LA CALIDAD DE LA FORMACIÓN OFRECIDA

2.1   Dimensión Perfil de Egreso y Resultados

2.1.2   Estructura Curricular

Console.WriteLine(ex.Message);

} }

3. Add the following using directives to your code:

using System.Data.SqlClient;

using System.IO;

4. Build and run the program. Check the My Documents folder for the file name CustomerList.txt and verify that the customer data has been written. Also verify the output on the Console window against the contents of the file.

The code in this exercise first opens a new StreamWriter object and calls its WriteLine method multiple times to write text to a text file. It then creates a StreamReader object to read text from the file that was just created by using the ReadLine method. When there is no data left to read, the ReadLine object returns a null value. The code uses this value to determine when to finish reading from the text file.

Working with XML

Extensible Markup Language (XML) is a text-based format for representing structured data.

In XML, you can store both data and metadata (information about the data being stored).

For example, the following XML represents data for two customers:

<?xml version="1.0" encoding="utf-8"?>

<!--Customer List-->

<Customers>

<Customer Id="ALFKI">

<CompanyName>Alfreds Futterkiste</CompanyName>

<Phone>030-0074321</Phone>

</Customer>

<Customer Id="EASTC">

<CompanyName>Eastern Connection</CompanyName>

<Phone>(171) 555-0297</Phone>

</Customer>

</Customers>

Even without knowing anything about XML, you can understand the contents of this file just by looking at them. XML consists of tags (contained within angle brackets) and data.

Tags always appear in pairs, with each opening tag matched by a closing tag. For example,

<Customers> is an opening tag, and </Customers> is the corresponding closing tag.

The first line of an XML document is the XML declaration:

<?xml version="1.0" encoding="utf-8"?>

XML tags that begin with <? are called processing instructions. This processing instruction tells us that the document is an XML document, conforms to the XML version 1.0 specifica-tions, and uses the UTF-8 character set for its data elements.

c06Understanding Databases.indd Page 167 2/28/11 3:21:20 PM f-392

c06Understanding Databases.indd Page 167 2/28/11 3:21:20 PM f-392 /Users/f-392/Desktop/Nalini 23.9/ch05/Users/f-392/Desktop/Nalini 23.9/ch05

168 | Lesson 6

An opening tag and closing tag together with their contents is called an element. For example, the following is a single XML element from the above document:

<Phone>030-0074321</Phone>

This bit of code defines an element with the name Phone whose value is 030-0074321.

Elements can be nested, but they cannot overlap. For example, the following XML is invalid because of the overlap between the CompanyName and Phone elements:

<Customer Id="EASTC">

<CompanyName>Eastern Connection<Phone>

</Phone>(171) 555-0297</CompanyName>

</Customer>

</Customers>

XML documents are hierarchical in nature. Every XML document contains a single root element that contains all the other nodes. An XML document can therefore be visualized as a tree of nodes.

Elements can contain attributes. An attribute is a piece of data that further describes an element. For example:

<Customer Id="ALFKI">

Here, the Customer element includes an attribute whose name is Id and whose value is ALFKI.

Finally, an XML document can contain comments. Comments start with the characters

<!-- and end with the characters -->.

XML is often more complex than what is discussed in this section. However, these basics are enough for you to understand most XML documents that you’ll likely run into until you start working with XML in depth.

There are many ways in which you can work with XML data. The classes that work with XML data are organized in the System.Xml namespace. This portion of the lesson focuses on the following commonly used classes:

• XmlReader and XmlWriter: These classes provide a fast, noncached, forward-only way to read or write XML data.

• XmlDocument: This class is an in-memory representation of XML data and allows navigation and editing of the XML document.

In the following exercise, you use the XmlReader class to read the XML file name Customers.

xml in a sequential and forward-only manner.

READ FROM AN XML FILE

GET READY. To read from an XML file, do the following:

1. Create a new Console Application project named WorkingWithXmlReader.

2. Add the following code to the Main method of the Program class:

using (XmlReader reader =

XmlReader.Create("Customers.xml")) {

while (reader.Read()) {

c06Understanding Databases.indd Page 168 2/28/11 3:21:20 PM f-392

c06Understanding Databases.indd Page 168 2/28/11 3:21:20 PM f-392 /Users/f-392/Desktop/Nalini 23.9/ch05/Users/f-392/Desktop/Nalini 23.9/ch05

Understanding Databases | 169

if (reader.IsStartElement()) {

switch (reader.Name) {

case "CompanyName":

if (reader.Read()) {

Console.Write(

"Company Name: {0}, ", reader.Value);

} break;

case "Phone":

if (reader.Read()) {

Console.WriteLine(

"Phone: {0}", reader.Value);

} break;

} } } }

3. Next, add the following using directive to the program:

using System.Xml;

4. Now, add a new XML file named Customers.xml to the project. Make sure the xml file contains the following data:

<?xml version="1.0" encoding="utf-8"?>

<!--Customer List-->

<Customers>

<Customer Id="ALFKI">

<CompanyName>Alfreds Futterkiste</CompanyName>

<Phone>030-0074321</Phone>

</Customer>

<Customer Id="EASTC">

<CompanyName>Eastern Connection</CompanyName>

<Phone>(171) 555-0297</Phone>

</Customer>

</Customers>

Build the program. Copy the Customers.xml file to the program executable folder. Run the program. You should see a list of all the company names and phone numbers.

c06Understanding Databases.indd Page 169 2/28/11 3:21:21 PM f-392

c06Understanding Databases.indd Page 169 2/28/11 3:21:21 PM f-392 /Users/f-392/Desktop/Nalini 23.9/ch05/Users/f-392/Desktop/Nalini 23.9/ch05

170 | Lesson 6

The code in this exercise first creates a new instance of XmlReader by using the XmlReader.

Create method. This will throw an exception if the file is not found. The program will termi-nate when the XmlReader.Read method has nothing to read. You can use properties such as Name and Value to access various portions of XML.