C# Tutorial MSM_Murach chapter-22-slides

26
Murach’s C# 2010, C22 © 2010, Mike Murach & Associates, Inc. Slide 1 C hapter22 How to w ork w ith XM L files

Transcript of C# Tutorial MSM_Murach chapter-22-slides

Page 1: C# Tutorial MSM_Murach chapter-22-slides

Murach’s C# 2010, C22 © 2010, Mike Murach & Associates, Inc. Slide 1

Chapter 22

How to work with XML files

Page 2: C# Tutorial MSM_Murach chapter-22-slides

Murach’s C# 2010, C22 © 2010, Mike Murach & Associates, Inc. Slide 2

Objectives Applied 1. Use the XmlWriter class to write an XML document. 2. Use the XmlReader class to read an XML document. 3. Use Visual Studio’s XML Editor to create and edit an XML

document in a project.

Knowledge 1. Identify the following in an XML document: XML declarations,

start tags, end tags, root elements, parent elements, child elements, attributes, element content, and comments.

Page 3: C# Tutorial MSM_Murach chapter-22-slides

Murach’s C# 2010, C22 © 2010, Mike Murach & Associates, Inc. Slide 3

Data for three products Code Description Price A3CS Murach’s ASP.NET 3.5 with C# 2008 54.50 JSE6 Murach’s Java SE 6 52.50 CS08 Murach’s C# 2008 54.50

Page 4: C# Tutorial MSM_Murach chapter-22-slides

Murach’s C# 2010, C22 © 2010, Mike Murach & Associates, Inc. Slide 4

An XML document that contains the product data <?xml version="1.0" encoding="utf-8" ?> <!--Product data--> <Products> <Product Code="A3CS"> <Description>Murach's ASP.NET 3.5 with C# 2008 </Description> <Price>54.50</Price> </Product> <Product Code="JSE6"> <Description>Murach's Java SE 6 </Description> <Price>52.50</Price> </Product> <Product Code="CS08"> <Description>Murach's C# 2008 </Description> <Price>54.50</Price> </Product> </Products>

Page 5: C# Tutorial MSM_Murach chapter-22-slides

Murach’s C# 2010, C22 © 2010, Mike Murach & Associates, Inc. Slide 5

An XML document <?xml version="1.0" encoding="utf-8" ?> <!--Product data--> <Products> <Product Code="CS08"> <Description>Murach's C# 2008</Description> <Price>54.50</Price> </Product> </Products>

Product element

Description element

Price element

Page 6: C# Tutorial MSM_Murach chapter-22-slides

Murach’s C# 2010, C22 © 2010, Mike Murach & Associates, Inc. Slide 6

Tags, XML declarations, and comments Each XML tag begins with < and ends with >. The first line in an XML document is an XML declaration that

indicates which version of the XML standard is being used for the document.

In addition, the declaration usually identifies the standard character set that’s being used. For documents in English-speaking countries, UTF-8 is the character set that’s commonly used.

You can use the <!-- and --> tags to include comments in an XML document.

Page 7: C# Tutorial MSM_Murach chapter-22-slides

Murach’s C# 2010, C22 © 2010, Mike Murach & Associates, Inc. Slide 7

Elements An element is a unit of XML data that begins with a start tag and

ends with an end tag. The start tag provides the name of the element and contains

any attributes assigned to the element. The end tag repeats the name, prefixed with a slash (/). The text between an element’s start and end tags is called the

element’s content. You can use any name you want for an XML element.

Elements can contain other elements. An element that’s contained within another is known as a child element. The element that contains a child element is known as the child’s parent element.

Child elements can repeat within a parent element. The highest-level parent element in an XML document is known

as the root element.

Page 8: C# Tutorial MSM_Murach chapter-22-slides

Murach’s C# 2010, C22 © 2010, Mike Murach & Associates, Inc. Slide 8

An XML document with a Code attribute <?xml version="1.0" encoding="utf-8" ?> <!--Product data--> <Products> <Product Code="CS08"> <Description>Murach's C# 2008</Description> <Price>54.50</Price> </Product> </Products>

Attributes You can include one or more attributes in the start tag for an

element. An attribute consists of an attribute name, an equal sign, and a

string value in quotes. If an element has more than one attribute, the order in which the

attributes appear doesn’t matter, but the attributes must be separated by one or more spaces.

Page 9: C# Tutorial MSM_Murach chapter-22-slides

Murach’s C# 2010, C22 © 2010, Mike Murach & Associates, Inc. Slide 9

When to use attributes instead of child elements When you design an XML document, you can use either a child

element or an attribute to represent a data item for an element. The choice of one over the other is often a matter of preference.

Two advantages of attributes are that they can appear in any order and they are more concise because they do not require end tags.

Two advantages of child elements are that they are easier for people to read and they are more convenient for long string values.

Page 10: C# Tutorial MSM_Murach chapter-22-slides

Murach’s C# 2010, C22 © 2010, Mike Murach & Associates, Inc. Slide 10

An XML document in the XML Editor

Page 11: C# Tutorial MSM_Murach chapter-22-slides

Murach’s C# 2010, C22 © 2010, Mike Murach & Associates, Inc. Slide 11

How to add a new XML file to a project Choose the ProjectAdd New Item command. In the Add New

Item dialog box, select the XML File template; type a name for the XML file in the Name text box; and click Open.

This adds an XML file to your project, adds the XML declaration for the document, and opens the document in the XML Editor.

How to open an existing XML file To open an existing XML file that’s stored in the project, double-

click on the file. To open an existing XML file without adding the file to your

project, use the FileOpen command.

Page 12: C# Tutorial MSM_Murach chapter-22-slides

Murach’s C# 2010, C22 © 2010, Mike Murach & Associates, Inc. Slide 12

How to edit an XML file When you type a start tag, the XML Editor automatically adds an

end tag for the element and positions the insertion point between the start and end tags so you can type the element’s content.

When you type an attribute, the XML Editor automatically adds a pair of quotation marks and positions the insertion point between them so you can type the attribute value.

Page 13: C# Tutorial MSM_Murach chapter-22-slides

Murach’s C# 2010, C22 © 2010, Mike Murach & Associates, Inc. Slide 13

Common methods of the XmlWriter class Create(path)

Create(path, settings)

WriteStartDocument()

WriteComment(comment)

WriteStartElement(elementName)

WriteAttributeString(attributeName, value)

WriteEndElement()

WriteElementString(elementName, content)

Close()

Common properties of the XmlWriterSettings class Indent

IndentChars

Page 14: C# Tutorial MSM_Murach chapter-22-slides

Murach’s C# 2010, C22 © 2010, Mike Murach & Associates, Inc. Slide 14

Code that writes an XML document // create the path variable string path = @"C:\C# 2010\Files\Products.xml"; // create the XmlWriterSettings object XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; settings.IndentChars = (" "); // create the XmlWriter object XmlWriter xmlOut = XmlWriter.Create(path, settings); // write the start of the document xmlOut.WriteStartDocument(); xmlOut.WriteStartElement("Products");

Page 15: C# Tutorial MSM_Murach chapter-22-slides

Murach’s C# 2010, C22 © 2010, Mike Murach & Associates, Inc. Slide 15

Code that writes an XML document (cont.) // write each Product object to the xml file foreach (Product product in products) { xmlOut.WriteStartElement("Product"); xmlOut.WriteAttributeString("Code", product.Code); xmlOut.WriteElementString("Description", product.Description); xmlOut.WriteElementString("Price", Convert.ToString(product.Price)); xmlOut.WriteEndElement(); } // write the end tag for the root element xmlOut.WriteEndElement(); // close the XmlWriter object xmlOut.Close();

Page 16: C# Tutorial MSM_Murach chapter-22-slides

Murach’s C# 2010, C22 © 2010, Mike Murach & Associates, Inc. Slide 16

Common indexer of the XmlReader class [name]

Common properties of the XmlReader class NodeType

Name

Value

EOF

Page 17: C# Tutorial MSM_Murach chapter-22-slides

Murach’s C# 2010, C22 © 2010, Mike Murach & Associates, Inc. Slide 17

Common methods of XmlReader class Create(path)

Create(path, settings)

Read()

ReadStartElement(name)

ReadEndElement()

ReadToDescendant(name) ReadToNextSibling(name) ReadElementContentAsString() ReadElementContentAsDecimal() Close()

Common properties of XmlReaderSettings class IgnoreWhitespace

IgnoreComments

Page 18: C# Tutorial MSM_Murach chapter-22-slides

Murach’s C# 2010, C22 © 2010, Mike Murach & Associates, Inc. Slide 18

An XML document <?xml version="1.0"> <!--Product data--> <Products> <Product Code="CS08"> <Description>Murach's C# 2008 </Description> <Price>54.50</Price> </Product> </Products>

Page 19: C# Tutorial MSM_Murach chapter-22-slides

Murach’s C# 2010, C22 © 2010, Mike Murach & Associates, Inc. Slide 19

The XML nodes in the products document NodeType Name Other properties XmlDeclaration xml Comment Value = “Product data” Element Products Element Product [“Code”] = “CS08” Element Description Text Value = “Murach’s C# 2008” EndElement Description Element Price Text Value = “54.50” EndElement Price EndElement Product EndElement Products

Page 20: C# Tutorial MSM_Murach chapter-22-slides

Murach’s C# 2010, C22 © 2010, Mike Murach & Associates, Inc. Slide 20

Code that reads an XML document // create the list List<Product> products = new List<Product>(); // create the path variable string path = @"C:\C# 2010\Files\Products.xml"; // create the XmlReaderSettings object XmlReaderSettings settings = new XmlReaderSettings(); settings.IgnoreWhitespace = true; settings.IgnoreComments = true; // create the XmlReader object XmlReader xmlIn = XmlReader.Create(path, settings);

Page 21: C# Tutorial MSM_Murach chapter-22-slides

Murach’s C# 2010, C22 © 2010, Mike Murach & Associates, Inc. Slide 21

Code that reads an XML document (cont.) // read past all nodes to the first Product node if {xmlIn.ReadToDescendant("Product")); { // create one Product object for each Product node do { Product product = new Product(); product.Code = xmlIn["Code"]; xmlIn.ReadStartElement("Product"); product.Description = xmlIn.ReadElementContentAsString(); product.Price = xmlIn.ReadElementContentAsDecimal(); products.Add(product); } while(xmlIn.ReadToNextSibling("Product")); } // close the XmlReader object xmlIn.Close();

Page 22: C# Tutorial MSM_Murach chapter-22-slides

Murach’s C# 2010, C22 © 2010, Mike Murach & Associates, Inc. Slide 22

A class that works with an XML document using System; using System.Xml; using System.Collections.Generic; namespace ProductMaintenance { public class ProductDB { private const string path = @"C:\C# 2010\Files\Products.xml"; public static List<Product> GetProducts() { // create the list List<Product> products = new List<Product>(); // create the XmlReaderSettings object XmlReaderSettings settings = new XmlReaderSettings(); settings.IgnoreWhitespace = true; settings.IgnoreComments = true;

Page 23: C# Tutorial MSM_Murach chapter-22-slides

Murach’s C# 2010, C22 © 2010, Mike Murach & Associates, Inc. Slide 23

A class that works with an XML document (cont.) // create the XmlReader object XmlReader xmlIn = XmlReader.Create( path, settings); // read past all nodes to first Product node if {xmlIn.ReadToDescendant("Product")); { // create object for each Product node do { Product product = new Product(); product.Code = xmlIn["Code"]; xmlIn.ReadStartElement("Product"); product.Description = xmlIn.ReadElementContentAsString(); product.Price = xmlIn.ReadElementContentAsDecimal(); products.Add(product); } while(xmlIn.ReadToNextSibling("Product")); }

Page 24: C# Tutorial MSM_Murach chapter-22-slides

Murach’s C# 2010, C22 © 2010, Mike Murach & Associates, Inc. Slide 24

A class that works with an XML document (cont.) // close the XmlReader object xmlIn.Close(); return products; } public static void SaveProducts( List<Product> products) { // create the XmlWriterSettings object XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; settings.IndentChars = (" "); // create the XmlWriter object XmlWriter xmlOut = XmlWriter.Create(path, settings);

Page 25: C# Tutorial MSM_Murach chapter-22-slides

Murach’s C# 2010, C22 © 2010, Mike Murach & Associates, Inc. Slide 25

A class that works with an XML document (cont.) // write the start of the document xmlOut.WriteStartDocument(); xmlOut.WriteStartElement("Products"); // write each Product object to the xml file foreach (Product product in products) { xmlOut.WriteStartElement("Product"); xmlOut.WriteAttributeString("Code", product.Code); xmlOut.WriteElementString("Description", product.Description); xmlOut.WriteElementString("Price", Convert.ToString(product.Price)); xmlOut.WriteEndElement(); }

Page 26: C# Tutorial MSM_Murach chapter-22-slides

Murach’s C# 2010, C22 © 2010, Mike Murach & Associates, Inc. Slide 26

A class that works with an XML document (cont.) // write the end tag for the root element xmlOut.WriteEndElement(); // close the XmlWriter object xmlOut.Close(); } } }