XML files Murach's C# 2015



Similar documents
Extending XSLT with Java and C#

Visual Basic. murach's TRAINING & REFERENCE

Terms and Definitions for CMS Administrators, Architects, and Developers

Exchanger XML Editor - Canonicalization and XML Digital Signatures

Appendix K Introduction to Microsoft Visual C++ 6.0

XSLT Mapping in SAP PI 7.1

Teradata SQL Assistant Version 13.0 (.Net) Enhancements and Differences. Mike Dempsey

C Coding Style Guide. Technotes, HowTo Series. 1 About the C# Coding Style Guide. 2 File Organization. Version 0.3. Contents

Intelligent Event Processer (IEP) Tutorial Detection of Insider Stock Trading

Programming with the Microsoft.NET Framework Using Microsoft Visual Studio 2005 (VB)

Chapter 1 Introduction to web development and PHP

Smooks Dev Tools Reference Guide. Version: GA

CSE 308. Coding Conventions. Reference

EDITOR & SITE TAGS

DEVELOPING CONTRACT - DRIVEN WEB SERVICES USING JDEVELOPER. The purpose of this tutorial is to develop a java web service using a top-down approach.

Alkacon Software GmbH


Java Software Development Kit (JDK 5.0 Update 14) Installation Step by Step Instructions

opencrx Language Localization Guide

The Google Web Toolkit (GWT): Declarative Layout with UiBinder Basics

Learn how to create web enabled (browser) forms in InfoPath 2013 and publish them in SharePoint InfoPath 2013 Web Enabled (Browser) forms

Web Services API Developer Guide

Hands-On Lab. Web Development in Visual Studio Lab version: Last updated: 12/10/2010. Page 1

CHAPTER 10: WEB SERVICES

Introduction to C#, Visual Studio and Windows Presentation Foundation

SECURE MOBILE ACCESS MODULE USER GUIDE EFT 2013

The presentation explains how to create and access the web services using the user interface. WebServices.ppt. Page 1 of 14

Android Studio Application Development

Appendix M: Introduction to Microsoft Visual C Express Edition

LAB 1. Familiarization of Rational Rose Environment And UML for small Java Application Development

CRM 2013 Workflows. Description

How To Create A Website Template On Sitefinity

Computer Programming In QBasic

Integrating InfoPath Forms. Paul Baker

Creating a Guest Book Using WebObjects Builder

umirror (version beta 0.9.0)

Vim, Emacs, and JUnit Testing. Audience: Students in CS 331 Written by: Kathleen Lockhart, CS Tutor

Instructor: Betty O Neil

Developing Web Views for VMware vcenter Orchestrator

Word 2010: Mail Merge to with Attachments

Data Integration through XML/XSLT. Presenter: Xin Gu

Preparing the PCT-EASY.zip for fully electronic filing with RO/US using EFS-Web. Version 6.0

About XML in InDesign

See the Developer s Getting Started Guide for an introduction to My Docs Online Secure File Delivery and how to use it programmatically.

Preparing the PCT-EASY.zip for fully electronic filing with RO/US using EFS-Web

Walkthrough: Creating and Using an ASP.NET Web Service in Visual Web Developer

STK Terrain Server Installation Guide

Listeners. Formats. Free Form. Formatted

Enhancing the SAS Enhanced Editor with Toolbar Customizations Lynn Mullins, PPD, Cincinnati, Ohio

Windows Server 2003 Logon Scripts Paul Flynn

Microsoft Expression Web Quickstart Guide

Examining the InDesign Server Solution

AxCMS.net on Network Load Balancing (NLB) Environment

XML & Databases. Tutorial. 2. Parsing XML. Universität Konstanz. Database & Information Systems Group Prof. Marc H. Scholl

Chapter 19: XML. Working with XML. About XML

CS506 Web Design and Development Solved Online Quiz No. 01

ACE: Dreamweaver CC Exam Guide

Web Application Architecture (based J2EE 1.4 Tutorial)

AutoMerge for MS CRM 3

metaengine DataConnect For SharePoint 2007 Configuration Guide

How to test and debug an ASP.NET application

Composite.Community.Newsletter - User Guide

Analytics Configuration Reference

SharePoint Integration Framework Developers Cookbook

Dreamweaver. Introduction to Editing Web Pages

Globalization and Localization

OpenROAD XML and Source Code Management

Data Tool Platform SQL Development Tools

DTD Tutorial. About the tutorial. Tutorial

Chapter 4 Accessing Data

Catalog Web service and catalog commerce management center customization

Hands-On: Introduction to Object-Oriented Programming in LabVIEW

Hypercosm. Studio.

Changing Your Cameleon Server IP

JBoss Portlet Container. User Guide. Release 2.0

PROCESSES LOADER 9.0 SETTING. Requirements and Assumptions: I. Requirements for the batch process:

Reference and Troubleshooting: FTP, IIS, and Firewall Information

An Introduction To The Web File Manager

Cleo Communications. CUEScript Training

Android Environment SDK

Novell Identity Manager

MOVES Batch Mode: Setting up and running groups of related MOVES run specifications. EPA Office of Transportation and Air Quality 11/3/2010

Notepad++ The COMPSCI 101 Text Editor for Windows. What is a text editor? Install Python 3

T-CONNECT EDI VIEWER. Installation and User Guide 12/16/2014 WE BUILD SOFTWARE THAT HELPS OUR CLIENTS GROW DOCUMENT CREATED BY:

Using SQL Server Management Studio

S. Bouzefrane. How to set up the Java Card development environment under Windows? Samia Bouzefrane.

Continuous Integration Part 2

CS 2112 Spring Instructions. Assignment 3 Data Structures and Web Filtering. 0.1 Grading. 0.2 Partners. 0.3 Restrictions

Using CertAgent to Obtain Domain Controller and Smart Card Logon Certificates for Active Directory Authentication

Xcode User Default Reference. (Legacy)

Expanded contents. Section 1. Chapter 2. The essence off ASP.NET web programming. An introduction to ASP.NET web programming


Development. with NetBeans 5.0. A Quick Start in Basic Web and Struts Applications. Geertjan Wielenga

Installing Java 5.0 and Eclipse on Mac OS X

INFOPATH FORMS FOR OUTLOOK, SHAREPOINT, OR THE WEB

Tutorial: Building a Dojo Application using IBM Rational Application Developer Loan Payment Calculator

Developing, Deploying, and Debugging Applications on Windows Embedded Standard 7

FTP, IIS, and Firewall Reference and Troubleshooting

Integrating with BarTender Integration Builder

Qlik REST Connector Installation and User Guide

Transcription:

XML files 2016, Mike Murach & Associates, Inc. C22, Slide 1

Data for three products Code Description Price A5CS Murach's ASP.NET 4.5 with C# 2012 57.50 BJWN Murach's Beginning Java with NetBeans 57.50 CS15 56.50 2016, Mike Murach & Associates, Inc. C22, Slide 2

An XML document that contains the data for the three products <?xml version="1.0" encoding="utf-8"?> <!--Product data--> <Products> <Product Code="A5CS"> <Description>Murach's ASP.NET 4.5 with C# 2012 </Description> <Price>57.50</Price> </Product> <Product Code="BJWN"> <Description>Murach's Beginning Java with NetBeans </Description> <Price>57.50</Price> </Product> <Product Code="CS15"> <Description></Description> <Price>56.50</Price> </Product> </Products> 2016, Mike Murach & Associates, Inc. C22, Slide 3

An XML document <?xml version="1.0" encoding="utf-8"?> <!--Product data--> <Products> <Product Code="CS15"> <Description></Description> <Price>56.50</Price> </Product> </Products> Product element Price element Description element 2016, Mike Murach & Associates, Inc. C22, Slide 4

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 Englishspeaking countries, UTF-8 is the character set that s commonly used. You can use the <!-- and --> tags to include comments in an XML document. 2016, Mike Murach & Associates, Inc. C22, Slide 5

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 element 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. An XML document can have only one root element. 2016, Mike Murach & Associates, Inc. C22, Slide 6

An XML document <?xml version="1.0" encoding="utf-8"?> <!--Product data--> <Products> <Product Code="CS15"> <Description></Description> <Price>56.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 equals 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. 2016, Mike Murach & Associates, Inc. C22, Slide 7

When to use attributes instead of child elements When you design an XML document, you can use either child elements or attributes to represent the data 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. 2016, Mike Murach & Associates, Inc. C22, Slide 8

An XML document in the XML Editor 2016, Mike Murach & Associates, Inc. C22, Slide 9

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 Add. 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, doubleclick on the file. To open an existing XML file without adding the file to your project, use the File Open File command. 2016, Mike Murach & Associates, Inc. C22, Slide 10

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 and an equals sign, the XML Editor automatically adds a pair of quotation marks and positions the insertion point between them so you can type the attribute value. 2016, Mike Murach & Associates, Inc. C22, Slide 11

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 2016, Mike Murach & Associates, Inc. C22, Slide 12

Code that writes an XML document // create the path variable string path = @"C:\C# 2015\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"); 2016, Mike Murach & Associates, Inc. C22, Slide 13

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(); 2016, Mike Murach & Associates, Inc. C22, Slide 14

Common indexer of the XmlReader class [name] Common properties of the XmlReader class NodeType Name Value EOF 2016, Mike Murach & Associates, Inc. C22, Slide 15

Common methods of the XmlReader class Create(path) Create(path, settings) Read() ReadStartElement(name) ReadEndElement() ReadToDescendant(name) ReadToNextSibling(name) ReadElementContentAsString() ReadElementContentAsDecimal() Close() Common properties of the XmlReaderSettings class IgnoreWhitespace IgnoreComments 2016, Mike Murach & Associates, Inc. C22, Slide 16

An XML document <?xml version="1.0" encoding="utf-8"?> <!--Product data--> <Products> <Product Code="CS15"> <Description></Description> <Price>56.50</Price> </Product> </Products> 2016, Mike Murach & Associates, Inc. C22, Slide 17

The XML nodes in this document NodeType Name Other properties XmlDeclaration xml Comment Value = Product data Element Products Element Product [ Code ] = CS15 Element Description Text Value = Murach s C# 2015 EndElement Description Element Price Text Value = 56.50 EndElement Price EndElement Product EndElement Products 2016, Mike Murach & Associates, Inc. C22, Slide 18

Code that reads an XML document // create the list List<Product> products = new List<Product>(); // create the path variable string path = @"C:\C# 2015\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); 2016, Mike Murach & Associates, Inc. C22, Slide 19

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(); 2016, Mike Murach & Associates, Inc. C22, Slide 20

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# 2015\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; 2016, Mike Murach & Associates, Inc. C22, Slide 21

A class that works with an XML document (cont.) // create the XmlReader object XmlReader xmlin = XmlReader.Create(path, settings); // 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")); } 2016, Mike Murach & Associates, Inc. C22, Slide 22

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); // write the start of the document xmlout.writestartdocument(); xmlout.writestartelement("products"); 2016, Mike Murach & Associates, Inc. C22, Slide 23

A class that works with 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(); 2016, Mike Murach & Associates, Inc. C22, Slide 24