SlideShare a Scribd company logo
1 of 26
Murach’s C# 2010, C22 © 2010, Mike Murach & Associates, Inc. Slide 1
Chapter 22
How to work with
XML files
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.
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
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>
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
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.
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.
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.
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.
Murach’s C# 2010, C22 © 2010, Mike Murach & Associates, Inc. Slide 10
An XML document in the XML Editor
Murach’s C# 2010, C22 © 2010, Mike Murach & Associates, Inc. Slide 11
How to add a new XML file to a project
• Choose the ProjectAdd 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 FileOpen command.
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.
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
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# 2010FilesProducts.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");
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();
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
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
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>
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
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# 2010FilesProducts.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);
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();
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# 2010FilesProducts.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;
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"));
}
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);
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();
}
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();
}
}
}

More Related Content

What's hot

C# Tutorial MSM_Murach chapter-24-slides
C# Tutorial MSM_Murach chapter-24-slidesC# Tutorial MSM_Murach chapter-24-slides
C# Tutorial MSM_Murach chapter-24-slidesSami Mut
 
C# Tutorial MSM_Murach chapter-17-slides
C# Tutorial MSM_Murach chapter-17-slidesC# Tutorial MSM_Murach chapter-17-slides
C# Tutorial MSM_Murach chapter-17-slidesSami Mut
 
C# Tutorial MSM_Murach chapter-03-slides
C# Tutorial MSM_Murach chapter-03-slidesC# Tutorial MSM_Murach chapter-03-slides
C# Tutorial MSM_Murach chapter-03-slidesSami Mut
 
C# Tutorial MSM_Murach chapter-12-slides
C# Tutorial MSM_Murach chapter-12-slidesC# Tutorial MSM_Murach chapter-12-slides
C# Tutorial MSM_Murach chapter-12-slidesSami Mut
 
C# Tutorial MSM_Murach chapter-18-slides
C# Tutorial MSM_Murach chapter-18-slidesC# Tutorial MSM_Murach chapter-18-slides
C# Tutorial MSM_Murach chapter-18-slidesSami Mut
 
Cookbook Oracle SOA Business Rules
Cookbook Oracle SOA Business RulesCookbook Oracle SOA Business Rules
Cookbook Oracle SOA Business RulesEmiel Paasschens
 
Chapter 2 — Program and Graphical User Interface Design
Chapter 2 — Program and Graphical User Interface DesignChapter 2 — Program and Graphical User Interface Design
Chapter 2 — Program and Graphical User Interface Designfrancopw
 
Visual basic ppt for tutorials computer
Visual basic ppt for tutorials computerVisual basic ppt for tutorials computer
Visual basic ppt for tutorials computersimran153
 
Visual Programming
Visual ProgrammingVisual Programming
Visual ProgrammingBagzzz
 
Visual Basic Programming
Visual Basic ProgrammingVisual Basic Programming
Visual Basic ProgrammingOsama Yaseen
 
Visual basic 6.0
Visual basic 6.0Visual basic 6.0
Visual basic 6.0sanket1996
 
Creating a quiz using visual basic 6
Creating a quiz using visual basic 6Creating a quiz using visual basic 6
Creating a quiz using visual basic 6Ella Marie Wico
 

What's hot (19)

C# Tutorial MSM_Murach chapter-24-slides
C# Tutorial MSM_Murach chapter-24-slidesC# Tutorial MSM_Murach chapter-24-slides
C# Tutorial MSM_Murach chapter-24-slides
 
C# Tutorial MSM_Murach chapter-17-slides
C# Tutorial MSM_Murach chapter-17-slidesC# Tutorial MSM_Murach chapter-17-slides
C# Tutorial MSM_Murach chapter-17-slides
 
C# Tutorial MSM_Murach chapter-03-slides
C# Tutorial MSM_Murach chapter-03-slidesC# Tutorial MSM_Murach chapter-03-slides
C# Tutorial MSM_Murach chapter-03-slides
 
C# Tutorial MSM_Murach chapter-12-slides
C# Tutorial MSM_Murach chapter-12-slidesC# Tutorial MSM_Murach chapter-12-slides
C# Tutorial MSM_Murach chapter-12-slides
 
C# Tutorial MSM_Murach chapter-18-slides
C# Tutorial MSM_Murach chapter-18-slidesC# Tutorial MSM_Murach chapter-18-slides
C# Tutorial MSM_Murach chapter-18-slides
 
Intake 38 8
Intake 38 8Intake 38 8
Intake 38 8
 
Cookbook Oracle SOA Business Rules
Cookbook Oracle SOA Business RulesCookbook Oracle SOA Business Rules
Cookbook Oracle SOA Business Rules
 
Vb6.0 intro
Vb6.0 introVb6.0 intro
Vb6.0 intro
 
Chapter 2 — Program and Graphical User Interface Design
Chapter 2 — Program and Graphical User Interface DesignChapter 2 — Program and Graphical User Interface Design
Chapter 2 — Program and Graphical User Interface Design
 
Visual programming
Visual programmingVisual programming
Visual programming
 
Visual basic ppt for tutorials computer
Visual basic ppt for tutorials computerVisual basic ppt for tutorials computer
Visual basic ppt for tutorials computer
 
Vb introduction.
Vb introduction.Vb introduction.
Vb introduction.
 
visual basic programming
visual basic programmingvisual basic programming
visual basic programming
 
Visual Programming
Visual ProgrammingVisual Programming
Visual Programming
 
Vb tutorial
Vb tutorialVb tutorial
Vb tutorial
 
Visual Basic Programming
Visual Basic ProgrammingVisual Basic Programming
Visual Basic Programming
 
Visual basic 6.0
Visual basic 6.0Visual basic 6.0
Visual basic 6.0
 
Creating a quiz using visual basic 6
Creating a quiz using visual basic 6Creating a quiz using visual basic 6
Creating a quiz using visual basic 6
 
Chapter03 Ppt
Chapter03 PptChapter03 Ppt
Chapter03 Ppt
 

Viewers also liked

C# Tutorial MSM_Murach chapter-25-slides
C# Tutorial MSM_Murach chapter-25-slidesC# Tutorial MSM_Murach chapter-25-slides
C# Tutorial MSM_Murach chapter-25-slidesSami Mut
 
C# Tutorial MSM_Murach chapter-19-slides
C# Tutorial MSM_Murach chapter-19-slidesC# Tutorial MSM_Murach chapter-19-slides
C# Tutorial MSM_Murach chapter-19-slidesSami Mut
 
C# Tutorial MSM_Murach chapter-23-slides
C# Tutorial MSM_Murach chapter-23-slidesC# Tutorial MSM_Murach chapter-23-slides
C# Tutorial MSM_Murach chapter-23-slidesSami Mut
 
C# Tutorial MSM_Murach chapter-15-slides
C# Tutorial MSM_Murach chapter-15-slidesC# Tutorial MSM_Murach chapter-15-slides
C# Tutorial MSM_Murach chapter-15-slidesSami Mut
 
How to Make Awesome SlideShares: Tips & Tricks
How to Make Awesome SlideShares: Tips & TricksHow to Make Awesome SlideShares: Tips & Tricks
How to Make Awesome SlideShares: Tips & TricksSlideShare
 
Getting Started With SlideShare
Getting Started With SlideShareGetting Started With SlideShare
Getting Started With SlideShareSlideShare
 

Viewers also liked (6)

C# Tutorial MSM_Murach chapter-25-slides
C# Tutorial MSM_Murach chapter-25-slidesC# Tutorial MSM_Murach chapter-25-slides
C# Tutorial MSM_Murach chapter-25-slides
 
C# Tutorial MSM_Murach chapter-19-slides
C# Tutorial MSM_Murach chapter-19-slidesC# Tutorial MSM_Murach chapter-19-slides
C# Tutorial MSM_Murach chapter-19-slides
 
C# Tutorial MSM_Murach chapter-23-slides
C# Tutorial MSM_Murach chapter-23-slidesC# Tutorial MSM_Murach chapter-23-slides
C# Tutorial MSM_Murach chapter-23-slides
 
C# Tutorial MSM_Murach chapter-15-slides
C# Tutorial MSM_Murach chapter-15-slidesC# Tutorial MSM_Murach chapter-15-slides
C# Tutorial MSM_Murach chapter-15-slides
 
How to Make Awesome SlideShares: Tips & Tricks
How to Make Awesome SlideShares: Tips & TricksHow to Make Awesome SlideShares: Tips & Tricks
How to Make Awesome SlideShares: Tips & Tricks
 
Getting Started With SlideShare
Getting Started With SlideShareGetting Started With SlideShare
Getting Started With SlideShare
 

Similar to C# Tutorial MSM_Murach chapter-22-slides (20)

Document object model
Document object modelDocument object model
Document object model
 
Webcomponents v2
Webcomponents v2Webcomponents v2
Webcomponents v2
 
Webcomponents TLV October 2014
Webcomponents TLV October 2014Webcomponents TLV October 2014
Webcomponents TLV October 2014
 
Web components
Web componentsWeb components
Web components
 
HTML literals, the JSX of the platform
HTML literals, the JSX of the platformHTML literals, the JSX of the platform
HTML literals, the JSX of the platform
 
XML Introduction
XML IntroductionXML Introduction
XML Introduction
 
ASP.NET MVC3 RAD
ASP.NET MVC3 RADASP.NET MVC3 RAD
ASP.NET MVC3 RAD
 
HTML practical file
HTML practical fileHTML practical file
HTML practical file
 
WEB TECHNOLOGY Unit-4.pptx
WEB TECHNOLOGY Unit-4.pptxWEB TECHNOLOGY Unit-4.pptx
WEB TECHNOLOGY Unit-4.pptx
 
Xml
XmlXml
Xml
 
Web engineering UNIT IV as per RGPV syllabus
Web engineering UNIT IV as per RGPV syllabusWeb engineering UNIT IV as per RGPV syllabus
Web engineering UNIT IV as per RGPV syllabus
 
MCA-202-W4-L1.pptx
MCA-202-W4-L1.pptxMCA-202-W4-L1.pptx
MCA-202-W4-L1.pptx
 
M.FLORENCE DAYANA WEB DESIGN -Unit 5 XML
M.FLORENCE DAYANA WEB DESIGN -Unit 5   XMLM.FLORENCE DAYANA WEB DESIGN -Unit 5   XML
M.FLORENCE DAYANA WEB DESIGN -Unit 5 XML
 
Xml11
Xml11Xml11
Xml11
 
Xml Presentation-1
Xml Presentation-1Xml Presentation-1
Xml Presentation-1
 
Unit 5 xml (1)
Unit 5   xml (1)Unit 5   xml (1)
Unit 5 xml (1)
 
Xml
XmlXml
Xml
 
Presentation html
Presentation   htmlPresentation   html
Presentation html
 
Dom structure
Dom structureDom structure
Dom structure
 
Dom structure
Dom structureDom structure
Dom structure
 

More from Sami Mut

C# Tutorial MSM_Murach chapter-13-slides
C# Tutorial MSM_Murach chapter-13-slidesC# Tutorial MSM_Murach chapter-13-slides
C# Tutorial MSM_Murach chapter-13-slidesSami Mut
 
C# Tutorial MSM_Murach chapter-11-slides
C# Tutorial MSM_Murach chapter-11-slidesC# Tutorial MSM_Murach chapter-11-slides
C# Tutorial MSM_Murach chapter-11-slidesSami Mut
 
C# Tutorial MSM_Murach chapter-04-slides
C# Tutorial MSM_Murach chapter-04-slidesC# Tutorial MSM_Murach chapter-04-slides
C# Tutorial MSM_Murach chapter-04-slidesSami Mut
 
C# Tutorial MSM_Murach chapter-09-slides
C# Tutorial MSM_Murach chapter-09-slidesC# Tutorial MSM_Murach chapter-09-slides
C# Tutorial MSM_Murach chapter-09-slidesSami Mut
 
C# Tutorial MSM_Murach chapter-08-slides
C# Tutorial MSM_Murach chapter-08-slidesC# Tutorial MSM_Murach chapter-08-slides
C# Tutorial MSM_Murach chapter-08-slidesSami Mut
 
C# Tutorial MSM_Murach chapter-07-slides
C# Tutorial MSM_Murach chapter-07-slidesC# Tutorial MSM_Murach chapter-07-slides
C# Tutorial MSM_Murach chapter-07-slidesSami Mut
 
C# Tutorial MSM_Murach chapter-06-slides
C# Tutorial MSM_Murach chapter-06-slidesC# Tutorial MSM_Murach chapter-06-slides
C# Tutorial MSM_Murach chapter-06-slidesSami Mut
 
chapter 5 Java at rupp cambodia
chapter 5 Java at rupp cambodiachapter 5 Java at rupp cambodia
chapter 5 Java at rupp cambodiaSami Mut
 
chapter 2 Java at rupp cambodia
chapter 2 Java at rupp cambodiachapter 2 Java at rupp cambodia
chapter 2 Java at rupp cambodiaSami Mut
 
chapter 3 Java at rupp cambodia
chapter 3 Java at rupp cambodiachapter 3 Java at rupp cambodia
chapter 3 Java at rupp cambodiaSami Mut
 
chapter 2 Java at rupp cambodia
chapter 2 Java at rupp cambodiachapter 2 Java at rupp cambodia
chapter 2 Java at rupp cambodiaSami Mut
 
chapter 1 Java at rupp cambodia
chapter 1 Java at rupp cambodiachapter 1 Java at rupp cambodia
chapter 1 Java at rupp cambodiaSami Mut
 

More from Sami Mut (13)

C# Tutorial MSM_Murach chapter-13-slides
C# Tutorial MSM_Murach chapter-13-slidesC# Tutorial MSM_Murach chapter-13-slides
C# Tutorial MSM_Murach chapter-13-slides
 
C# Tutorial MSM_Murach chapter-11-slides
C# Tutorial MSM_Murach chapter-11-slidesC# Tutorial MSM_Murach chapter-11-slides
C# Tutorial MSM_Murach chapter-11-slides
 
C# Tutorial MSM_Murach chapter-04-slides
C# Tutorial MSM_Murach chapter-04-slidesC# Tutorial MSM_Murach chapter-04-slides
C# Tutorial MSM_Murach chapter-04-slides
 
C# Tutorial MSM_Murach chapter-09-slides
C# Tutorial MSM_Murach chapter-09-slidesC# Tutorial MSM_Murach chapter-09-slides
C# Tutorial MSM_Murach chapter-09-slides
 
C# Tutorial MSM_Murach chapter-08-slides
C# Tutorial MSM_Murach chapter-08-slidesC# Tutorial MSM_Murach chapter-08-slides
C# Tutorial MSM_Murach chapter-08-slides
 
C# Tutorial MSM_Murach chapter-07-slides
C# Tutorial MSM_Murach chapter-07-slidesC# Tutorial MSM_Murach chapter-07-slides
C# Tutorial MSM_Murach chapter-07-slides
 
C# Tutorial MSM_Murach chapter-06-slides
C# Tutorial MSM_Murach chapter-06-slidesC# Tutorial MSM_Murach chapter-06-slides
C# Tutorial MSM_Murach chapter-06-slides
 
MSM_Time
MSM_TimeMSM_Time
MSM_Time
 
chapter 5 Java at rupp cambodia
chapter 5 Java at rupp cambodiachapter 5 Java at rupp cambodia
chapter 5 Java at rupp cambodia
 
chapter 2 Java at rupp cambodia
chapter 2 Java at rupp cambodiachapter 2 Java at rupp cambodia
chapter 2 Java at rupp cambodia
 
chapter 3 Java at rupp cambodia
chapter 3 Java at rupp cambodiachapter 3 Java at rupp cambodia
chapter 3 Java at rupp cambodia
 
chapter 2 Java at rupp cambodia
chapter 2 Java at rupp cambodiachapter 2 Java at rupp cambodia
chapter 2 Java at rupp cambodia
 
chapter 1 Java at rupp cambodia
chapter 1 Java at rupp cambodiachapter 1 Java at rupp cambodia
chapter 1 Java at rupp cambodia
 

Recently uploaded

Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDGMarianaLemus7
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 

Recently uploaded (20)

Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDG
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 

C# Tutorial MSM_Murach chapter-22-slides

  • 1. Murach’s C# 2010, C22 © 2010, Mike Murach & Associates, Inc. Slide 1 Chapter 22 How to work with XML files
  • 2. 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.
  • 3. 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
  • 4. 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>
  • 5. 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
  • 6. 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.
  • 7. 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.
  • 8. 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.
  • 9. 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.
  • 10. Murach’s C# 2010, C22 © 2010, Mike Murach & Associates, Inc. Slide 10 An XML document in the XML Editor
  • 11. Murach’s C# 2010, C22 © 2010, Mike Murach & Associates, Inc. Slide 11 How to add a new XML file to a project • Choose the ProjectAdd 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 FileOpen command.
  • 12. 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.
  • 13. 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
  • 14. 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# 2010FilesProducts.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");
  • 15. 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();
  • 16. 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
  • 17. 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
  • 18. 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>
  • 19. 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
  • 20. 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# 2010FilesProducts.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);
  • 21. 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();
  • 22. 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# 2010FilesProducts.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;
  • 23. 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")); }
  • 24. 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);
  • 25. 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(); }
  • 26. 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(); } } }