XML Mark Sapossnek CS 594 Computer Science Department

  • Slides: 97
Download presentation
XML Mark Sapossnek CS 594 Computer Science Department Metropolitan College Boston University

XML Mark Sapossnek CS 594 Computer Science Department Metropolitan College Boston University

Jump Table Added for CSE 681 w w w w Components of an XML

Jump Table Added for CSE 681 w w w w Components of an XML Document Syntax and Structure An XML Document Syntax and Structure Namespaces: Overview The XML ‘Alphabet Soup’ Transformations: XSL XPath (XML Path Language) XML in. NET Core Classes in System. XML

Learning Objectives w w Learn what XML is Learn the various ways in which

Learning Objectives w w Learn what XML is Learn the various ways in which XML is used Learn the key companion technologies Learn how to use the. NET framework to read, write, and navigate XML documents

Agenda w w w Overview Syntax and Structure The XML Alphabet Soup XML in.

Agenda w w w Overview Syntax and Structure The XML Alphabet Soup XML in. NET Relational Data and XML

Overview What is XML? w w A tag-based meta language Designed for structured data

Overview What is XML? w w A tag-based meta language Designed for structured data representation Represents data hierarchically (in a tree) Provides context to data (makes it meaningful) n Self-describing data w Separates presentation (HTML) from data (XML) w An open W 3 C standard w A subset of SGML n vs. HTML, which is an implementation of SGML

Overview What is XML? w XML is a “use everywhere” data specification XML Application

Overview What is XML? w XML is a “use everywhere” data specification XML Application X Documents XML Repository XML Database Configuration

Overview Documents vs. Data w XML is used to represent two main types of

Overview Documents vs. Data w XML is used to represent two main types of things: n Documents l n Lots of text with tags to identify and annotate portions of the document Data l Hierarchical data structures

Overview XML and Structured Data w Pre-XML representation of data: “PO-1234”, ”CUST 001”, ”X

Overview XML and Structured Data w Pre-XML representation of data: “PO-1234”, ”CUST 001”, ”X 9876”, ” 5”, ” 14. 98” w XML representation of the same data: <PURCHASE_ORDER> <PO_NUM> PO-1234 </PO_NUM> <CUST_ID> CUST 001 </CUST_ID> <ITEM_NUM> X 9876 </ITEM_NUM> <QUANTITY> 5 </QUANTITY> <PRICE> 14. 98 </PRICE> </PURCHASE_ORDER>

Overview Benefits of XML w Open W 3 C standard w Representation of data

Overview Benefits of XML w Open W 3 C standard w Representation of data across heterogeneous environments n n Cross platform Allows for high degree of interoperability w Strict rules n n n Syntax Structure Case sensitive

Overview Who Uses XML? w Submissions by n n n Microsoft IBM Hewlett-Packard Fujitsu

Overview Who Uses XML? w Submissions by n n n Microsoft IBM Hewlett-Packard Fujitsu Laboratories Sun Microsystems Netscape (AOL), and others… w Technologies using XML n SOAP, eb. XML, Biz. Talk, Web. Sphere, many others…

Agenda w w w Overview Syntax and Structure The XML Alphabet Soup XML in.

Agenda w w w Overview Syntax and Structure The XML Alphabet Soup XML in. NET Relational Data and XML

Syntax and Structure Components of an XML Document w Elements n Each element has

Syntax and Structure Components of an XML Document w Elements n Each element has a beginning and ending tag l n <TAG_NAME>. . . </TAG_NAME> Elements can be empty (<TAG_NAME />) w Attributes n n Describes an element; e. g. data type, data range, etc. Can only appear on beginning tag w Processing instructions n n n Encoding specification (Unicode by default) Namespace declaration Schema declaration

Syntax and Structure Components of an XML Document <? xml version=“ 1. 0” ?

Syntax and Structure Components of an XML Document <? xml version=“ 1. 0” ? > <? xml-stylesheet type="text/xsl" href=“template. xsl"? > <ROOT> <ELEMENT 1><SUBELEMENT 1 /><SUBELEMENT 2 /></ELEMENT 1> <ELEMENT 2> </ELEMENT 2> <ELEMENT 3 type=‘string’> </ELEMENT 3> <ELEMENT 4 type=‘integer’ value=‘ 9. 3’> </ELEMENT 4> </ROOT> Elements with Attributes Elements Prologue (processing instructions)

Syntax and Structure Rules For Well-Formed XML w There must be one, and only

Syntax and Structure Rules For Well-Formed XML w There must be one, and only one, root element w Sub-elements must be properly nested n A tag must end within the tag in which it was started w Attributes are optional n Defined by an optional schema w Attribute values must be enclosed in “” or ‘’ w Processing instructions are optional w XML is case-sensitive n <tag> and <TAG> are not the same type of element

Syntax and Structure Well-Formed XML? w No, CHILD 2 and CHILD 3 do not

Syntax and Structure Well-Formed XML? w No, CHILD 2 and CHILD 3 do not nest propertly <xml? Version=“ 1. 0” ? > <PARENT> <CHILD 1>This is element 1</CHILD 1> <CHILD 2><CHILD 3>Number 3</CHILD 2></CHILD 3> </PARENT>

Syntax and Structure Well-Formed XML? w No, there are two root elements <xml? Version=“

Syntax and Structure Well-Formed XML? w No, there are two root elements <xml? Version=“ 1. 0” ? > <PARENT> <CHILD 1>This is element 1</CHILD 1> </PARENT> <CHILD 1>This is another element 1</CHILD 1> </PARENT>

Syntax and Structure Well-Formed XML? w Yes <xml? Version=“ 1. 0” ? > <PARENT>

Syntax and Structure Well-Formed XML? w Yes <xml? Version=“ 1. 0” ? > <PARENT> <CHILD 1>This is element 1</CHILD 1> <CHILD 2/> <CHILD 3></CHILD 3> </PARENT>

Syntax and Structure An XML Document <? xml version='1. 0'? > <bookstore> <book genre=‘autobiography’

Syntax and Structure An XML Document <? xml version='1. 0'? > <bookstore> <book genre=‘autobiography’ publicationdate=‘ 1981’ ISBN=‘ 1 -861003 -11 -0’> <title>The Autobiography of Benjamin Franklin</title> <author> <first-name>Benjamin</first-name> <last-name>Franklin</last-name> </author> <price>8. 99</price> </book> <book genre=‘novel’ publicationdate=‘ 1967’ ISBN=‘ 0 -201 -63361 -2’> <title>The Confidence Man</title> <author> <first-name>Herman</first-name> <last-name>Melville</last-name> </author> <price>11. 99</price> </bookstore>

Syntax and Structure Namespaces: Overview w Part of XML’s extensibility w Allow authors to

Syntax and Structure Namespaces: Overview w Part of XML’s extensibility w Allow authors to differentiate between tags of the same name (using a prefix) n n Frees author to focus on the data and decide how to best describe it Allows multiple XML documents from multiple authors to be merged w Identified by a URI (Uniform Resource Identifier) n When a URL is used, it does NOT have to represent a live server

Syntax and Structure Namespaces: Declaration Namespace declaration examples: xmlns: bk = “http: //www. example.

Syntax and Structure Namespaces: Declaration Namespace declaration examples: xmlns: bk = “http: //www. example. com/bookinfo/” xmlns: bk = “urn: mybookstuff. org: bookinfo” xmlns: bk = “http: //www. example. com/bookinfo/” Namespace declaration Prefix URI (URL)

Syntax and Structure Namespaces: Examples <BOOK xmlns: bk=“http: //www. bookstuff. org/bookinfo”> <bk: TITLE>All About

Syntax and Structure Namespaces: Examples <BOOK xmlns: bk=“http: //www. bookstuff. org/bookinfo”> <bk: TITLE>All About XML</bk: TITLE> <bk: AUTHOR>Joe Developer</bk: AUTHOR> <bk: PRICE currency=‘US Dollar’>19. 99</bk: PRICE> <bk: BOOK xmlns: bk=“http: //www. bookstuff. org/bookinfo” xmlns: money=“urn: finance: money”> <bk: TITLE>All About XML</bk: TITLE> <bk: AUTHOR>Joe Developer</bk: AUTHOR> <bk: PRICE money: currency=‘US Dollar’> 19. 99</bk: PRICE>

Syntax and Structure Namespaces: Default Namespace w An XML namespace declared without a prefix

Syntax and Structure Namespaces: Default Namespace w An XML namespace declared without a prefix becomes the default namespace for all sub-elements w All elements without a prefix will belong to the default namespace: <BOOK xmlns=“http: //www. bookstuff. org/bookinfo”> <TITLE>All About XML</TITLE> <AUTHOR>Joe Developer</AUTHOR>

Syntax and Structure Namespaces: Scope w Unqualified elements belong to the inner-most default namespace.

Syntax and Structure Namespaces: Scope w Unqualified elements belong to the inner-most default namespace. n n BOOK, TITLE, and AUTHOR belong to the default book namespace PUBLISHER and NAME belong to the default publisher namespace <BOOK xmlns=“www. bookstuff. org/bookinfo”> <TITLE>All About XML</TITLE> <AUTHOR>Joe Developer</AUTHOR> <PUBLISHER xmlns=“urn: publishers: publinfo”> <NAME>Microsoft Press</NAME> </PUBLISHER> </BOOK>

Syntax and Structure Namespaces: Attributes w Unqualified attributes do NOT belong to any namespace

Syntax and Structure Namespaces: Attributes w Unqualified attributes do NOT belong to any namespace n Even if there is a default namespace w This differs from elements, which belong to the default namespace

Syntax and Structure Entities w Entities provide a mechanism for textual substitution, e. g.

Syntax and Structure Entities w Entities provide a mechanism for textual substitution, e. g. Entity Substitution < < & & w You can define your own entities w Parsed entities can contain text and markup w Unparsed entities can contain any data n JPEG photos, GIF files, movies, etc.

Agenda w w w Overview Syntax and Structure The XML Alphabet Soup XML in.

Agenda w w w Overview Syntax and Structure The XML Alphabet Soup XML in. NET Relational Data and XML

The XML ‘Alphabet Soup’ w XML itself is fairly simple w Most of the

The XML ‘Alphabet Soup’ w XML itself is fairly simple w Most of the learning curve is knowing about all of the related technologies

The XML ‘Alphabet Soup’ XML Extensible Markup Language Defines XML documents Infoset Information Set

The XML ‘Alphabet Soup’ XML Extensible Markup Language Defines XML documents Infoset Information Set Abstract model of XML data; definition of terms DTD Document Type Definition Non-XML schema XSD XML Schema XML-based schema language XDR XML Data Reduced An earlier XML schema CSS Cascading Style Sheets Allows you to specify styles XSL Extensible Stylesheet Language for expressing stylesheets; consists of XSLT and XSL-FO XSLT XSL Transformations Language for transforming XML documents XSL-FO XSL Formatting Objects Language to describe precise layout of text on a page

The XML ‘Alphabet Soup’ XPath XML Path Language XPointer XML Pointer Language XLink XML

The XML ‘Alphabet Soup’ XPath XML Path Language XPointer XML Pointer Language XLink XML Linking Language XQuery DOM SAX Data Island A language for addressing parts of an XML document, designed to be used by both XSLT and XPointer Supports addressing into the internal structures of XML documents Describes links between XML documents XML Query Language Flexible mechanism for querying XML (draft) data as if it were a database Document Object Model API to read, create and edit XML documents; creates in-memory object model Simple API for XML API to parse XML documents; eventdriven XML data embedded in a HTML page Data Binding Automatic population of HTML elements from XML data

The XML ‘Alphabet Soup’ Schemas: Overview w DTD (Document Type Definitions) n n Not

The XML ‘Alphabet Soup’ Schemas: Overview w DTD (Document Type Definitions) n n Not written in XML No support for data types or namespaces w XSD (XML Schema Definition) n n n Written in XML Supports data types Current standard recommended by W 3 C w XDR (XML Data Reduced schema) n n Interim schema proposed by Microsoft Obsoleted by XSD

The XML ‘Alphabet Soup’ Schemas: Purpose w Define the “rules” (grammar) of the document

The XML ‘Alphabet Soup’ Schemas: Purpose w Define the “rules” (grammar) of the document n n Data types Value bounds w A XML document that conforms to a schema is said to be valid n More restrictive than well-formed XML w Define which elements are present and in what order w Define the structural relationships of elements

The XML ‘Alphabet Soup’ Schemas: DTD Example w XML document: <BOOK> <TITLE>All About XML</TITLE>

The XML ‘Alphabet Soup’ Schemas: DTD Example w XML document: <BOOK> <TITLE>All About XML</TITLE> <AUTHOR>Joe Developer</AUTHOR> </BOOK> w DTD schema: <!DOCTYPE <!ELEMENT ]> BOOK TITLE AUTHOR [ (TITLE+, AUTHOR) > (#PCDATA) >

The XML ‘Alphabet Soup’ Schemas: XSD Example w XML document: <CATALOG> <BOOK> <TITLE>All About

The XML ‘Alphabet Soup’ Schemas: XSD Example w XML document: <CATALOG> <BOOK> <TITLE>All About XML</TITLE> <AUTHOR>Joe Developer</AUTHOR> </BOOK> … </CATALOG>

The XML ‘Alphabet Soup’ Schemas: XSD Example <xsd: schema id="New. Data. Set“ target. Namespace="http:

The XML ‘Alphabet Soup’ Schemas: XSD Example <xsd: schema id="New. Data. Set“ target. Namespace="http: //tempuri. org/schema 1. xsd" xmlns: xsd="http: //www. w 3. org/1999/XMLSchema" xmlns: msdata="urn: schemas-microsoft-com: xml-msdata"> <xsd: element name="book"> <xsd: complex. Type content="element. Only"> <xsd: all> <xsd: element name="title" min. Occurs="0" type="xsd: string"/> <xsd: element name="author" min. Occurs="0" type="xsd: string"/> </xsd: all> </xsd: complex. Type> </xsd: element> <xsd: element name=“Catalog" msdata: Is. Data. Set="True"> <xsd: complex. Type> <xsd: choice max. Occurs="unbounded"> <xsd: element ref="book"/> </xsd: choice> </xsd: complex. Type> </xsd: element> </xsd: schema>

The XML ‘Alphabet Soup’ Schemas: Why You Should Use XSD w Newest W 3

The XML ‘Alphabet Soup’ Schemas: Why You Should Use XSD w Newest W 3 C Standard w Broad support for data types w Reusable “components” n n w w w Simple data types Complex data types Extensible Inheritance support Namespace support Ability to map to relational database tables XSD support in Visual Studio. NET

The XML ‘Alphabet Soup’ Transformations: XSL w Language for expressing document styles w Specifies

The XML ‘Alphabet Soup’ Transformations: XSL w Language for expressing document styles w Specifies the presentation of XML n More powerful than CSS w Consists of: n n n XSLT XPath XSL Formatting Objects (XSL-FO)

The XML ‘Alphabet Soup’ Transformations: Overview w XSLT – a language used to transform

The XML ‘Alphabet Soup’ Transformations: Overview w XSLT – a language used to transform XML data into a different form (commonly XML or HTML) XML, HTML, … XSLT

The XML ‘Alphabet Soup’ Transformations: XSLT w The language used for converting XML documents

The XML ‘Alphabet Soup’ Transformations: XSLT w The language used for converting XML documents into other forms w Describes how the document is transformed w Expressed as an XML document (. xsl) w Template rules n n Patterns match nodes in source document Templates instantiated to form part of result document w Uses XPath for querying, sorting, etc.

The XML ‘Alphabet Soup’ Transformations: Example <sales> <summary> <heading>Scootney Publishing</heading> <subhead>Regional Sales Report</subhead> <description>Sales

The XML ‘Alphabet Soup’ Transformations: Example <sales> <summary> <heading>Scootney Publishing</heading> <subhead>Regional Sales Report</subhead> <description>Sales Report</description> </summary> <data> <region> <name>West Coast</name> <quarter number="1" books_sold="24000" <quarter number="2" books_sold="38600" <quarter number="3" books_sold="44030" <quarter number="4" books_sold="21000" </region>. . . </data> </sales> /> />

The XML ‘Alphabet Soup’ Transformations: Example <xsl: param name="low_sales" select="21000"/> <BODY> <h 1><xsl: value-of

The XML ‘Alphabet Soup’ Transformations: Example <xsl: param name="low_sales" select="21000"/> <BODY> <h 1><xsl: value-of select="//summary/heading"/></h 1>. . . <table><tr><th>RegionQuarter</th> <xsl: for-each select="//data/region[1]/quarter"> <th>Q<xsl: value-of select="@number"/></th> </xsl: for-each>. . . <xsl: for-each select="//data/region"> <tr><xsl: value-of select="name"/></th> <xsl: for-each select="quarter"> <td><xsl: choose> <xsl: when test="number(@books_sold < = $low_sales)"> color: red; </xsl: when> <xsl: otherwise>color: green; </xsl: otherwise></xsl: choose> <xsl: value-of select="format-number(@books_sold, '###, ###')"/></td>. . . <td><xsl: value-of select="format-number(sum(quarter/@books_sold), '###, ###')"/>

The XML ‘Alphabet Soup’ Transformations: Example

The XML ‘Alphabet Soup’ Transformations: Example

The XML ‘Alphabet Soup’ XSL Formatting Objects (XSL-FO) w A set of formatting semantics

The XML ‘Alphabet Soup’ XSL Formatting Objects (XSL-FO) w A set of formatting semantics w Denotes typographic elements (for example: page, paragraph, rule, etc. ) w Allows finer control obtained via formatting elements n n Word, letter spacing Indentation Widow, orphan, hyphenation control Font style, etc.

The XML ‘Alphabet Soup’ XPath (XML Path Language) w General purpose query language for

The XML ‘Alphabet Soup’ XPath (XML Path Language) w General purpose query language for identifying nodes in an XML document w Declarative (vs. procedural) w Contextual – the results depend on current node w Supports standard comparison, Boolean and mathematical operators (=, <, and, or, *, +, etc. )

The XML ‘Alphabet Soup’ XPath Operators Operator Usage Description / Child operator – selects

The XML ‘Alphabet Soup’ XPath Operators Operator Usage Description / Child operator – selects only immediate children (when at the beginning of the pattern, context is root) // Recursive descent – selects elements at any depth (when at the beginning of the pattern, context is root) . Indicates current context * Wildcard @ Prefix to attribute name (when alone, it is an attribute wildcard) [ ] Applies filter pattern

The XML ‘Alphabet Soup’ XPath Query Examples . /author (finds all author elements within

The XML ‘Alphabet Soup’ XPath Query Examples . /author (finds all author elements within current context) /bookstore (find the bookstore element at the root) /* (find the root element) //author (find all author elements anywhere in document) /bookstore[@specialty = “textbooks”] (find all bookstores where the specialty attribute = “textbooks”) /book[@style = /bookstore/@specialty] (find all books where the style attribute = the specialty attribute of the bookstore element at the root)

The XML ‘Alphabet Soup’ XPointer w Builds upon XPath to: n n n Identify

The XML ‘Alphabet Soup’ XPointer w Builds upon XPath to: n n n Identify sub-node data Identify a range of data Identify data in local document or remote documents w New standard

The XML ‘Alphabet Soup’ XLink w XML Linking Language w Elements of XML documents

The XML ‘Alphabet Soup’ XLink w XML Linking Language w Elements of XML documents w Describes links between resources n n Simple links (for example, HTML HREFs) Extended links l l l Remote resources Local resources Rules for how a link is followed, etc.

The XML ‘Alphabet Soup’ The XML DOM w XML Document Object Model (DOM) w

The XML ‘Alphabet Soup’ The XML DOM w XML Document Object Model (DOM) w Provides a programming interface for manipulating XML documents in memory w Includes a set of objects and interfaces that represent the content and structure of an XML document w Enables a program to traverse an XML tree w Allows elements, attributes, etc. , to be added/deleted in an XML tree w Allows new XML documents to be created programmatically

The XML ‘Alphabet Soup’ SAX (Simple API for XML) w API to allow developers

The XML ‘Alphabet Soup’ SAX (Simple API for XML) w API to allow developers to read/write XML data w Event based n Uses a “push” model w Sequential access only (data not cached) w Requires less memory to process XML data than the DOM n n SAX has less overhead (uses small input, work and output buffers) than the DOM constructs the data structure in memory (work and output buffers = to size of data)

The XML ‘Alphabet Soup’ Data Islands w XML embedded in an HTML document w

The XML ‘Alphabet Soup’ Data Islands w XML embedded in an HTML document w Manipulated via client side script or data binding <XML id=“XMLID”> <BOOK> <TITLE>All About XML</TITLE> <AUTHOR>Joe Developer</AUTHOR> </BOOK> </XML> <XML id=“XMLID” src=“mydocument. xml”>

The XML ‘Alphabet Soup’ Data Islands w Can be embedded in an HTML SCRIPT

The XML ‘Alphabet Soup’ Data Islands w Can be embedded in an HTML SCRIPT element w XML is accessible via the DOM: <SCRIPT language=“xml” id=“XMLID”> <SCRIPT type=“text/xml” id=“XMLID”> <SCRIPT language=“xml” id=“XMLID” src=“mydocument. xml”>

The XML ‘Alphabet Soup’ Data Islands w Access the XML via the HTML DOM:

The XML ‘Alphabet Soup’ Data Islands w Access the XML via the HTML DOM: function return. XMLData() { return document. all("XMLID"). XMLDocument. node. Value; } w Or access the XML directly via the ID: function return. XMLData() { return XMLID. document. Element. text; }

The XML ‘Alphabet Soup’ Data Binding w Client-side data binding (in the browser) w

The XML ‘Alphabet Soup’ Data Binding w Client-side data binding (in the browser) w The XML Data Source Object (DSO) binds HTML elements to an XML data set (or data island) w When the XML data set changes, the bound elements are updated dynamically w DATASRC: the source of the data (e. g. , the ID of the data island) w DATAFLD: the field (XML element) to display w Can offload XSLT processing to the client w IE only

The XML ‘Alphabet Soup’ Data Binding <HTML><BODY> <XML ID="xml. Parts"> <? xml version="1. 0"

The XML ‘Alphabet Soup’ Data Binding <HTML><BODY> <XML ID="xml. Parts"> <? xml version="1. 0" ? > <parts> <partnumber>A 1000</partnumber> <description>Flat washer</description> <quantity>1000</quantity> </part>. . . </XML> <table datasrc=#xml. Parts border=1><tr> <td><div datafld="partnumber"></div></td> <td><div datafld="quantity"></div></td> </tr></table> </BODY></HTML>

The XML ‘Alphabet Soup’ XLang (Biz. Talk) w An XML language for defining processes

The XML ‘Alphabet Soup’ XLang (Biz. Talk) w An XML language for defining processes w Processes usually on multiple platforms w Support for: n n Concurrency Long-running transactions w Exposed messaging and orchestration APIs n n n Connect to COM components Connect to MSMQ queues Connect to SQL components

The XML ‘Alphabet Soup’ XML-Based Applications w Microsoft Biz. Talk Server n n Enables

The XML ‘Alphabet Soup’ XML-Based Applications w Microsoft Biz. Talk Server n n Enables uniform exchange of data among disparate systems (XLang) Backend interchange with partners/customers w Microsoft Commerce Server n n XML-based Product Catalog System Integrated with Biz. Talk Server for backend communication

The XML ‘Alphabet Soup’ XML-Based Applications w Microsoft SQL Server n n Retrieve relational

The XML ‘Alphabet Soup’ XML-Based Applications w Microsoft SQL Server n n Retrieve relational data as XML Query XML data Join XML data with existing database tables Update the database via XML Updategrams w Microsoft Exchange Server n n XML is native representation of many types of data Used to enhance performance of UI scenarios (for example, Outlook Web Access (OWA))

Agenda w w w Overview Syntax and Structure The XML Alphabet Soup XML in.

Agenda w w w Overview Syntax and Structure The XML Alphabet Soup XML in. NET Relational Data and XML

XML in. NET Overview w XML use is ubiquitous throughout. NET w Web Services

XML in. NET Overview w XML use is ubiquitous throughout. NET w Web Services n Based on XML standards: SOAP, WSDL, UDDI, … w ASP. NET n Application information stored in XML-based configuration files w ADO. NET n n Provides conversion between Data. Sets and XML Data. Sets are serialized as XML

XML in. NET Overview w XML classes built on industry standards n e. g.

XML in. NET Overview w XML classes built on industry standards n e. g. , DOM Level 2 Core, XML Schemas, SOAP w Introduces a stream-based (“pull”) interface n vs. the traditional SAX “push” model w Natural evolution of MSXML 3. 0 (4. 0) n MSXML 3. 0 still available in. NET via COM Interop w. NET XML classes are well-factored for functionality, performance and extensibility

XML in. NET Core Classes in System. XML Abstract (Base) Class Concrete (Derived) Class

XML in. NET Core Classes in System. XML Abstract (Base) Class Concrete (Derived) Class Xml. Node Xml. Document Xml. Linked. Node Xml. Element Xml. Attribute Xml. Data. Document Xml. Reader Xml. Text. Reader Xml. Node. Reader Xml. Writer Xml. Text. Writer

XML in. NET Xml. Node w Represents a single node in a XML document

XML in. NET Xml. Node w Represents a single node in a XML document hierarchy w An abstract class w Properties and methods to n n traverse XML document hierarchy query properties modify nodes delete notes

XML in. NET Xml. Document w Implements W 3 C XML Document Object Model

XML in. NET Xml. Document w Implements W 3 C XML Document Object Model (DOM) Level 1 and Level 2 specifications w Implements Xml. Node w Represents an entire XML document n All nodes are available to view/manipulate w Cached in memory w Xml. Document is analogous to a Data. Set w Events for changing, inserting and removing nodes

XML in. NET Xml. Document w Declaration: public class Xml. Document : Xml. Node

XML in. NET Xml. Document w Declaration: public class Xml. Document : Xml. Node w Constructor: public Xml. Document(); w Code example: Xml. Document my. Xml. Doc = new Xml. Document(); my. Xml. Doc. Load(“c: Sample. xml”);

XML in. NET Xml. Linked. Node w Implements Xml. Node w Retrieves the node

XML in. NET Xml. Linked. Node w Implements Xml. Node w Retrieves the node immediately preceding or following the current node w An abstract class from which Xml. Element is derived n Declaration: public abstract class Xml. Linked. Node : Xml. Node

XML in. NET Xml. Element w Represents an element in the DOM tree w

XML in. NET Xml. Element w Represents an element in the DOM tree w Properties and methods to view, modify, and create element objects n Declaration: public class Xml. Element : Xml. Linked. Node n Code example: Xml. Document my. Xml. Doc = new Xml. Document(); my. Xml. Doc. Load (“c: Sample. xml”); // Document. Element retrieves the root element Xml. Element root = my. Xml. Document. Element;

XML in. NET Xml. Attribute w Implements Xml. Node w Represents an attribute of

XML in. NET Xml. Attribute w Implements Xml. Node w Represents an attribute of an Xml. Element w Valid and/or default values defined by schema n Declaration: public class Xml. Attribute : Xml. Node

XML in. NET Xml. Attribute w Code example: Xml. Document my. Xml. Doc =

XML in. NET Xml. Attribute w Code example: Xml. Document my. Xml. Doc = new Xml. Document(); my. Xml. Doc. Load (“c: Sample. xml”); // Get the attribute collection of the root element Xml. Attribute. Collection attr. Coll = my. Xml. Document. Element. Attributes; // Create a new attribute and set it’s value Xml. Attribute new. Attr = my. Xml. Doc. Create. Attribute(“value”); new. Attr. Value = “new value”; // Append the new attribute to the collection attr. Coll. Append(new. Attr);

XML in. NET Demo Xml. Document

XML in. NET Demo Xml. Document

XML in. NET Xml. Node: IXPath. Navigable w Xml. Node has simple methods for

XML in. NET Xml. Node: IXPath. Navigable w Xml. Node has simple methods for traversing a XML document hierarchy w Xml. Node is an interface for accessing XML data using a cursor model w Provides a generic navigation mechanism w Support for XPath expressions through IXPath. Navigable interface w Can select and navigate a subset of a document w Properties and methods to view, modify, copy, delete nodes

XML in. NET XPath. Navigator w Allows navigation over a Xml. Document w Xml.

XML in. NET XPath. Navigator w Allows navigation over a Xml. Document w Xml. Document is derived from Xml. Node, which implements IXPath. Navigable public class Xml. Document : XMLNode { XMLNode IXPath. Navigable. Create. Navigator() { } } w Caveats n n “Current” node remains current when moved Can be in a null state when not pointing to a node Does not “walk” off end of tree Failed methods leave XPath. Navigator where it was

XML in. NET XPath. Navigator w Declaration: public class XPath. Navigator : IXPath. Navigable

XML in. NET XPath. Navigator w Declaration: public class XPath. Navigator : IXPath. Navigable w Constructor: public XPath. Navigator (Xml. Document document); w Code example: Xml. Document my. Xml. Doc; my. Xml. Doc. Load (“c: Sample. xml”); XPath. Navigator nav = my. Xml. Doc. Create. Navigator(); nav. Move. To. Root(); nav. Move. To. Next(); // move to root element // move to next element

XML in. NET XPath. Navigator w Example: iterate over a subset of nodes Xml.

XML in. NET XPath. Navigator w Example: iterate over a subset of nodes Xml. Document doc = new Xml. Document(); doc. Load("person. xml"); XPath. Navigator nav = doc. Create. Navigator(); XPath. Node. Iterator iter = nav. Select("/person/name"); while (iter. Move. To. Next ()) { // process selection here… with iter. Current. Value } w Example: sum all Prices in a document public static void Sum. Price. Nodes(XPath. Navigator nav) { // in this case, evaluate returns a number Console. Write. Line("sum=" + nav. Evaluate("sum(//Price)")); }

XML in. NET Demo XPath. Navigator

XML in. NET Demo XPath. Navigator

XML in. NET Xml. Reader w Xml. Reader is an abstract class that provides

XML in. NET Xml. Reader w Xml. Reader is an abstract class that provides fast, non-cached, forward-only, read-only access to XML data w Uses a “pull” model n n Simpler than the SAX “push” model User can easily implement “push” model if desired l But the reverse is not true w Reads in depth-first order n Same order as textual XML data w Analogous to an ADO. NET Data. Reader

XML in. NET Xml. Text. Reader w Forward-only, read-only, non-cached access to stream-based XML

XML in. NET Xml. Text. Reader w Forward-only, read-only, non-cached access to stream-based XML data w Implements Xml. Reader w Access to data by parsing text input from: n n n Streams Text. Reader objects Strings w Properties and methods to view elements and attributes w Event support for validation

XML in. NET Xml. Text. Reader w Declaration: public class Xml. Text. Reader :

XML in. NET Xml. Text. Reader w Declaration: public class Xml. Text. Reader : Xml. Reader w Constructor (overloaded): public Xml. Text. Reader(Text. Reader textreader); public Xml. Text. Reader(string url); public Xml. Text. Reader(Stream stream); w Code example: Xml. Text. Reader reader = new Xml. Text. Reader(“c: Sample. xml”); While (reader. Read()) {. . . }

XML in. NET Xml. Node. Reader w w Forward-only, non-cached access to XML data

XML in. NET Xml. Node. Reader w w Forward-only, non-cached access to XML data Implements Xml. Reader Access to data via XPath. Navigator Reads data from an entire Xml. Document or a specific Xml. Node w Properties and methods to view elements and attributes

XML in. NET Xml. Writer w Abstract class providing fast, non-cached, forward-only, write-only creation

XML in. NET Xml. Writer w Abstract class providing fast, non-cached, forward-only, write-only creation of XML data w Makes it easy to create well-formed XML data in a type-safe manner

XML in. NET Xml. Text. Writer w Forward-only, non-cached method of writing XML data

XML in. NET Xml. Text. Writer w Forward-only, non-cached method of writing XML data to a stream w Implements Xml. Writer w Can write to: n n n Streams Files Text. Writer objects w Properties and methods to write syntactically valid XML

XML in. NET Xml. Text. Writer w Declaration: public class Xml. Text. Writer :

XML in. NET Xml. Text. Writer w Declaration: public class Xml. Text. Writer : Xml. Writer w Constructor (overloaded): public Xml. Text. Writer(Text. Writer textwriter); public Xml. Text. Reader(string file, Encoding encoding); public Xml. Text. Writer(Stream stream, Encoding encoding); w Code example: Xml. Text. Writer writer = new Xml. Text. Writer (“c: Sample. xml”, null); writer. Write. Start. Document(); // Write the declaration writer. Write. Start. Element(“ROOT”); // Write the root element writer. Write. End. Element(); // Write the close tag of root writer. Flush(); // Writer XML to file writer. Close(); // Close writer

XML in. NET Demo Xml. Text. Reader & Xml. Text. Writer

XML in. NET Demo Xml. Text. Reader & Xml. Text. Writer

XML in. NET Other XML Namespaces w System. Xml – Core XML namespace w

XML in. NET Other XML Namespaces w System. Xml – Core XML namespace w System. Xml. XPath – contains the XPath. Navigator, XPath parser and evaluation engine w System. Xml. Xsl – support XSLT transformations w System. Xml. Serialization – Classes to serialize objects into XML documents or streams w System. Xml. Schema – Supports XSD schemas

Agenda w w w Overview Syntax and Structure The XML Alphabet Soup XML in.

Agenda w w w Overview Syntax and Structure The XML Alphabet Soup XML in. NET Relational Data and XML

Relational Data and XML Relational Support in System. Xml w w w Xml. Data.

Relational Data and XML Relational Support in System. Xml w w w Xml. Data. Document XPath. Navigator Build a relation on a Data. Set Built-in XML support in SQL Server 2000

Relational Data and XML Xml. Data. Document w Extends Xml. Document to include support

Relational Data and XML Xml. Data. Document w Extends Xml. Document to include support for relational data (files, RDBMS, etc. ) w Provides a Data. Set property, which is kept synchronized with the XML data w Schema support n n Schema can be loaded/saved as XSD Schema can be inferred from XML data

Relational Data and XML Xml. Data. Document Controls, Designers, Code-gen, etc. XSL/T, X-Path, Validation,

Relational Data and XML Xml. Data. Document Controls, Designers, Code-gen, etc. XSL/T, X-Path, Validation, etc. Xml. Data. Document Data. Set Sync Xml. Reader Data. Adapter Xml. Text- Xml. Node. Reader Sql. Data- Ole. Db. Data. Adapter Data. Reader Sql. Data- Ole. Db. Data. Reader

Relational Data and XML Xml. Data. Document data. Doc = new Xml. Data. Document();

Relational Data and XML Xml. Data. Document data. Doc = new Xml. Data. Document(); // Read the XML into a Data. Set data. Doc. Data. Set. Read. Xml(new Stream. Reader(“c: example. xml”); foreach(Data. Table table in data. Doc. Data. Set. Tables) { foreach(Data. Column column in table. Columns) { } foreach(Data. Row row in table. Rows) { foreach(Object value in row. Item. Array) { } } }

Relational Data and XML Data. Document. Navigator w Implements Xml. Navigator over an Xml.

Relational Data and XML Data. Document. Navigator w Implements Xml. Navigator over an Xml. Data. Document w Contains the features of Document. Navigator n n Cursor model access XPath support, etc. w Includes support for relational data (Xml. Data. Document)

Relational Data and XML Building a Data. Set Relation w Create Data. Set w

Relational Data and XML Building a Data. Set Relation w Create Data. Set w Define tables: Data. Set dataset = new Data. Set(); dataset. Name = "Book. Authors"; Data. Table authors = new Data. Table("Author"); Data. Table books = new Data. Table("Book");

Relational Data and XML Building a Data. Set Relation w Define columns w Define

Relational Data and XML Building a Data. Set Relation w Define columns w Define keys: Data. Column id = authors. Columns. Add("ID", typeof(Int 32)); id. Auto. Increment = true; authors. Primary. Key = new Data. Column[] {id}; Data. Column name = new authors. Columns. Add("Name", typeof(String)); Data. Column isbn = books. Columns. Add("ISBN", typeof(String)); books. Primary. Key = new Data. Column[] {isbn}; Data. Column title = books. Columns. Add("Title", typeof(String)); Data. Column authid = books. Columns. Add(“Auth. ID”, typeof(Int 32)); Data. Column[] foreignkey = new Data. Column[] {authid};

Relational Data and XML Building a Data. Set Relation w Add the tables to

Relational Data and XML Building a Data. Set Relation w Add the tables to the Data. Set: dataset. Tables. Add (authors); dataset. Tables. Add (books);

Relational Data and XML Building a Data. Set Relation w Add data and save

Relational Data and XML Building a Data. Set Relation w Add data and save the Data. Set: Data. Row shkspr = authors. New. Row(); shkspr["Name"] = "William Shakespeare"; authors. Rows. Add(shkspr); Data. Relation bookauth = new Data. Relation("Book. Authors", authors. Primary. Key, foreignkey); dataset. Relations. Add (bookauth); Data. Row row = books. New. Row(); row["Auth. ID"] = shkspr["ID"]; row["ISBN"] = "1000 -XYZ"; row["Title"] = "Mac. Beth"; books. Rows. Add(row); dataset. Accept. Changes();

Relational Data and XML Support in SQL Server 2000 w w Ability to query

Relational Data and XML Support in SQL Server 2000 w w Ability to query SQL Server over HTTP Retrieve XML using SELECT … FOR XML Retrieve XML using XPath queries Write XML data using OPENXML rowset provider

Conclusion w w w Overview Syntax and Structure The XML Alphabet Soup XML in.

Conclusion w w w Overview Syntax and Structure The XML Alphabet Soup XML in. NET Relational Data and XML

Resources w http: //msdn. microsoft. com/xml/ w http: //www. xml. com/ w http: //www.

Resources w http: //msdn. microsoft. com/xml/ w http: //www. xml. com/ w http: //www. w 3. org/xml/ w Microsoft Press Books (http: //mspress. microsoft. com/) n n n XML Step By Step XML In Action Developing XML Solutions w O’Reilly Press n XML In A Nutshell

Resources w XML in. NET http: //msdn. microsoft. com/msdnmag/issues/ 01/01/xml. asp w Working with

Resources w XML in. NET http: //msdn. microsoft. com/msdnmag/issues/ 01/01/xml. asp w Working with XML in the. NET Platform http: //www. xmlmag. com/upload/free/feature s/xml/2001/05 may 01/dw 0102. asp