Title: XML
1XML
- Mark SapossnekCS 594
- Computer Science Department
- Metropolitan College
- Boston University
2Jump TableAdded for CSE681
- 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
3Learning Objectives
- 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
4Agenda
- Overview
- Syntax and Structure
- The XML Alphabet Soup
- XML in .NET
- Relational Data and XML
5OverviewWhat is XML?
- A tag-based meta language
- Designed for structured data representation
- Represents data hierarchically (in a tree)
- Provides context to data (makes it meaningful)
- Self-describing data
- Separates presentation (HTML) from data (XML)
- An open W3C standard
- A subset of SGML
- vs. HTML, which is an implementation of SGML
6OverviewWhat is XML?
- XML is a use everywhere data specification
XML
XML
XML
XML
7OverviewDocuments vs. Data
- XML is used to represent two main types of
things - Documents
- Lots of text with tags to identify and annotate
portions of the document - Data
- Hierarchical data structures
8OverviewXML and Structured Data
- Pre-XML representation of data
- XML representation of the same data
PO-1234,CUST001,X9876,5,14.98
ltPURCHASE_ORDERgt ltPO_NUMgt PO-1234
lt/PO_NUMgt ltCUST_IDgt CUST001 lt/CUST_IDgt ltITEM_NUM
gt X9876 lt/ITEM_NUMgt ltQUANTITYgt 5
lt/QUANTITYgt ltPRICEgt 14.98 lt/PRICEgt lt/PURCHASE_ORD
ERgt
9OverviewBenefits of XML
- Open W3C standard
- Representation of data across heterogeneous
environments - Cross platform
- Allows for high degree of interoperability
- Strict rules
- Syntax
- Structure
- Case sensitive
10OverviewWho Uses XML?
- Submissions by
- Microsoft
- IBM
- Hewlett-Packard
- Fujitsu Laboratories
- Sun Microsystems
- Netscape (AOL), and others
- Technologies using XML
- SOAP, ebXML, BizTalk, WebSphere, many others
11Agenda
- Overview
- Syntax and Structure
- The XML Alphabet Soup
- XML in .NET
- Relational Data and XML
12Syntax and StructureComponents of an XML Document
- Elements
- Each element has a beginning and ending tag
- ltTAG_NAMEgt...lt/TAG_NAMEgt
- Elements can be empty (ltTAG_NAME /gt)
- Attributes
- Describes an element e.g. data type, data range,
etc. - Can only appear on beginning tag
- Processing instructions
- Encoding specification (Unicode by default)
- Namespace declaration
- Schema declaration
13Syntax and StructureComponents of an XML Document
- lt?xml version1.0 ?gt
- lt?xml-stylesheet type"text/xsl"
hreftemplate.xsl"?gt - ltROOTgt
- ltELEMENT1gtltSUBELEMENT1 /gtltSUBELEMENT2
/gtlt/ELEMENT1gt - ltELEMENT2gt lt/ELEMENT2gt
- ltELEMENT3 typestringgt lt/ELEMENT3gt
- ltELEMENT4 typeinteger value9.3gt
lt/ELEMENT4gt - lt/ROOTgt
Elements with Attributes
Elements
Prologue (processing instructions)
14Syntax and StructureRules For Well-Formed XML
- There must be one, and only one, root element
- Sub-elements must be properly nested
- A tag must end within the tag in which it was
started - Attributes are optional
- Defined by an optional schema
- Attribute values must be enclosed in or
- Processing instructions are optional
- XML is case-sensitive
- lttaggt and ltTAGgt are not the same type of element
15Syntax and StructureWell-Formed XML?
- No, CHILD2 and CHILD3 do not nest propertly
ltxml? Version1.0 ?gt ltPARENTgt ltCHILD1gtThis is
element 1lt/CHILD1gt ltCHILD2gtltCHILD3gtNumber
3lt/CHILD2gtlt/CHILD3gt lt/PARENTgt
16Syntax and StructureWell-Formed XML?
- No, there are two root elements
ltxml? Version1.0 ?gt ltPARENTgt ltCHILD1gtThis is
element 1lt/CHILD1gt lt/PARENTgt ltPARENTgt ltCHILD1gtThi
s is another element 1lt/CHILD1gt lt/PARENTgt
17Syntax and StructureWell-Formed XML?
ltxml? Version1.0 ?gt ltPARENTgt ltCHILD1gtThis is
element 1lt/CHILD1gt ltCHILD2/gt ltCHILD3gtlt/CHILD3gt lt
/PARENTgt
18Syntax and StructureAn XML Document
lt?xml version'1.0'?gt ltbookstoregt ltbook
genreautobiography publicationdate1981
ISBN1-861003-11-0gt lttitlegtThe
Autobiography of Benjamin Franklinlt/titlegt
ltauthorgt ltfirst-namegtBenjaminlt/first-namegt
ltlast-namegtFranklinlt/last-namegt
lt/authorgt ltpricegt8.99lt/pricegt lt/bookgt
ltbook genrenovel publicationdate1967
ISBN0-201-63361-2gt lttitlegtThe Confidence
Manlt/titlegt ltauthorgt ltfirst-namegtHermanlt
/first-namegt ltlast-namegtMelvillelt/last-namegt
lt/authorgt ltpricegt11.99lt/pricegt
lt/bookgt lt/bookstoregt
19Syntax and Structure Namespaces Overview
- Part of XMLs extensibility
- Allow authors to differentiate between tags of
the same name (using a prefix) - Frees author to focus on the data and decide how
to best describe it - Allows multiple XML documents from multiple
authors to be merged - Identified by a URI (Uniform Resource Identifier)
- When a URL is used, it does NOT have to represent
a live server
20Syntax and Structure Namespaces Declaration
Namespace declaration examples
xmlns bk http//www.example.com/bookinfo/
xmlns bk urnmybookstuff.orgbookinfo
xmlns bk http//www.example.com/bookinfo/
Namespace declaration
Prefix
URI (URL)
21Syntax and Structure Namespaces Examples
ltBOOK xmlnsbkhttp//www.bookstuff.org/bookinfo
gt ltbkTITLEgtAll About XMLlt/bkTITLEgt
ltbkAUTHORgtJoe Developerlt/bkAUTHORgt ltbkPRICE
currencyUS Dollargt19.99lt/bkPRICEgt
ltbkBOOK xmlnsbkhttp//www.bookstuff.org/bookin
fo xmlnsmoneyurnfinancemoneygt
ltbkTITLEgtAll About XMLlt/bkTITLEgt
ltbkAUTHORgtJoe Developerlt/bkAUTHORgt ltbkPRICE
moneycurrencyUS Dollargt
19.99lt/bkPRICEgt
22Syntax and Structure Namespaces Default
Namespace
- An XML namespace declared without a prefix
becomes the default namespace for all
sub-elements - All elements without a prefix will belong to the
default namespace
ltBOOK xmlnshttp//www.bookstuff.org/bookinfogt
ltTITLEgtAll About XMLlt/TITLEgt ltAUTHORgtJoe
Developerlt/AUTHORgt
23Syntax and Structure Namespaces Scope
- Unqualified elements belong to the inner-most
default namespace. - BOOK, TITLE, and AUTHOR belong to the default
book namespace - PUBLISHER and NAME belong to the default
publisher namespace
ltBOOK xmlnswww.bookstuff.org/bookinfogt
ltTITLEgtAll About XMLlt/TITLEgt ltAUTHORgtJoe
Developerlt/AUTHORgt ltPUBLISHER
xmlnsurnpublisherspublinfogt
ltNAMEgtMicrosoft Presslt/NAMEgt
lt/PUBLISHERgt lt/BOOKgt
24Syntax and Structure Namespaces Attributes
- Unqualified attributes do NOT belong to any
namespace - Even if there is a default namespace
- This differs from elements, which belong to the
default namespace
25Syntax and Structure Entities
- Entities provide a mechanism for textual
substitution, e.g. -
- You can define your own entities
- Parsed entities can contain text and markup
- Unparsed entities can contain any data
- JPEG photos, GIF files, movies, etc.
Entity Substitution
lt lt
amp
26Agenda
- Overview
- Syntax and Structure
- The XML Alphabet Soup
- XML in .NET
- Relational Data and XML
27The XML Alphabet Soup
- XML itself is fairly simple
- Most of the learning curve is knowing about all
of the related technologies
28The 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 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
29The XML Alphabet Soup
XPath XML Path Language A language for addressing parts of an XML document, designed to be used by both XSLT and XPointer
XPointer XML Pointer Language Supports addressing into the internal structures of XML documents
XLink XML Linking Language Describes links between XML documents
XQuery XML Query Language (draft) Flexible mechanism for querying XML data as if it were a database
DOM Document Object Model API to read, create and edit XML documents creates in-memory object model
SAX Simple API for XML API to parse XML documents event-driven
Data Island XML data embedded in a HTML page XML data embedded in a HTML page
Data Binding Automatic population of HTML elements from XML data Automatic population of HTML elements from XML data
30The XML Alphabet Soup Schemas Overview
- DTD (Document Type Definitions)
- Not written in XML
- No support for data types or namespaces
- XSD (XML Schema Definition)
- Written in XML
- Supports data types
- Current standard recommended by W3C
- XDR (XML Data Reduced schema)
- Interim schema proposed by Microsoft
- Obsoleted by XSD
31The XML Alphabet Soup Schemas Purpose
- Define the rules (grammar) of the document
- Data types
- Value bounds
- A XML document that conforms to a schema is said
to be valid - More restrictive than well-formed XML
- Define which elements are present and in what
order - Define the structural relationships of elements
32The XML Alphabet Soup Schemas DTD Example
ltBOOKgt ltTITLEgtAll About XMLlt/TITLEgt
ltAUTHORgtJoe Developerlt/AUTHORgt lt/BOOKgt
lt!DOCTYPE BOOK lt!ELEMENT BOOK (TITLE, AUTHOR)
gt lt!ELEMENT TITLE (PCDATA) gt lt!ELEMENT
AUTHOR (PCDATA) gt gt
33The XML Alphabet Soup Schemas XSD Example
ltCATALOGgt ltBOOKgt ltTITLEgtAll About
XMLlt/TITLEgt ltAUTHORgtJoe Developerlt/AUTHORgt
lt/BOOKgt lt/CATALOGgt
34The XML Alphabet Soup Schemas XSD Example
ltxsdschema id"NewDataSet targetNamespace"http
//tempuri.org/schema1.xsd" xmlns"http//tempu
ri.org/schema1.xsd" xmlnsxsd"http//www.w3.o
rg/1999/XMLSchema" xmlnsmsdata"urnschemas-mi
crosoft-comxml-msdata"gt ltxsdelement
name"book"gt ltxsdcomplexType
content"elementOnly"gt ltxsdallgt
ltxsdelement name"title" minOccurs"0"
type"xsdstring"/gt ltxsdelement
name"author" minOccurs"0" type"xsdstring"/gt
lt/xsdallgt lt/xsdcomplexTypegt
lt/xsdelementgt ltxsdelement nameCatalog"
msdataIsDataSet"True"gt ltxsdcomplexTypegt
ltxsdchoice maxOccurs"unbounded"gt
ltxsdelement ref"book"/gt lt/xsdchoicegt
lt/xsdcomplexTypegt lt/xsdelementgt lt/xsdschemagt
35The XML Alphabet Soup Schemas Why You Should
Use XSD
- Newest W3C Standard
- Broad support for data types
- Reusable components
- Simple data types
- Complex data types
- Extensible
- Inheritance support
- Namespace support
- Ability to map to relational database tables
- XSD support in Visual Studio.NET
36The XML Alphabet Soup Transformations XSL
- Language for expressing document styles
- Specifies the presentation of XML
- More powerful than CSS
- Consists of
- XSLT
- XPath
- XSL Formatting Objects (XSL-FO)
37The XML Alphabet Soup Transformations Overview
- XSLT a language used to transform XML data into
a different form (commonly XML or HTML)
XML
XML,HTML,
XSLT
38The XML Alphabet Soup Transformations XSLT
- The language used for converting XML documents
into other forms - Describes how the document is transformed
- Expressed as an XML document (.xsl)
- Template rules
- Patterns match nodes in source document
- Templates instantiated to form part of result
document - Uses XPath for querying, sorting, etc.
39The XML Alphabet Soup Transformations Example
ltsalesgt ltsummarygt ltheadinggtScootney
Publishinglt/headinggt ltsubheadgtRegional Sales
Reportlt/subheadgt ltdescriptiongtSales
Reportlt/descriptiongt lt/summarygt ltdatagt
ltregiongt ltnamegtWest Coastlt/namegt
ltquarter number"1" books_sold"24000" /gt
ltquarter number"2" books_sold"38600" /gt
ltquarter number"3" books_sold"44030" /gt
ltquarter number"4" books_sold"21000" /gt
lt/regiongt ... lt/datagt lt/salesgt
40The XML Alphabet Soup Transformations Example
ltxslparam name"low_sales" select"21000"/gt ltBODY
gt lth1gtltxslvalue-of select"//summary/heading"/gtlt
/h1gt ... lttablegtlttrgtltthgtRegion\Quarterlt/thgt
ltxslfor-each select"//data/region1/quarter"gt
ltthgtQltxslvalue-of select"_at_number"/gtlt/thgt
lt/xslfor-eachgt ... ltxslfor-each
select"//data/region"gt lttrgtltxslvalue-of
select"name"/gtlt/thgt ltxslfor-each
select"quarter"gt lttdgtltxslchoosegt
ltxslwhen test"number(_at_books_sold lt
low_sales)"gt colorredlt/xslwhengt
ltxslotherwisegtcolorgreenlt/xslotherwisegtlt/xs
lchoosegt ltxslvalue-of select"format-number
(_at_books_sold,',')"/gtlt/tdgt ...
lttdgtltxslvalue-of select"format-number(
sum(quarter/_at_books_sold),',')"/gt
41The XML Alphabet Soup Transformations Example
42The XML Alphabet SoupXSL Formatting Objects
(XSL-FO)
- A set of formatting semantics
- Denotes typographic elements (for example page,
paragraph, rule, etc.) - Allows finer control obtained via formatting
elements - Word, letter spacing
- Indentation
- Widow, orphan, hyphenation control
- Font style, etc.
43The XML Alphabet Soup XPath (XML Path Language)
- General purpose query language for identifying
nodes in an XML document - Declarative (vs. procedural)
- Contextual the results depend on current node
- Supports standard comparison, Boolean and
mathematical operators (, lt, and, or, , , etc.)
44The 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
_at_ Prefix to attribute name (when alone, it is an attribute wildcard)
Applies filter pattern
45The 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_at_specialty
textbooks (find all bookstores where the
specialty attribute
textbooks) /book_at_style /bookstore/_at_specialty
(find all books where the style
attribute the specialty attribute
of the bookstore element at the root)
46The XML Alphabet Soup XPointer
- Builds upon XPath to
- Identify sub-node data
- Identify a range of data
- Identify data in local document or remote
documents - New standard
47The XML Alphabet Soup XLink
- XML Linking Language
- Elements of XML documents
- Describes links between resources
- Simple links (for example, HTML HREFs)
- Extended links
- Remote resources
- Local resources
- Rules for how a link is followed, etc.
48The XML Alphabet Soup The XML DOM
- XML Document Object Model (DOM)
- Provides a programming interface for manipulating
XML documents in memory - Includes a set of objects and interfaces that
represent the content and structure of an XML
document - Enables a program to traverse an XML tree
- Allows elements, attributes, etc., to be
added/deleted in an XML tree - Allows new XML documents to be created
programmatically
49The XML Alphabet Soup SAX (Simple API for XML)
- API to allow developers to read/write XML data
- Event based
- Uses a push model
- Sequential access only (data not cached)
- Requires less memory to process XML data than the
DOM - SAX has less overhead (uses small input, work and
output buffers) than the DOM - DOM constructs the data structure in memory (work
and output buffers to size of data)
50The XML Alphabet Soup Data Islands
- XML embedded in an HTML document
- Manipulated via client side script or data binding
ltXML idXMLIDgt ltBOOKgt ltTITLEgtAll About
XMLlt/TITLEgt ltAUTHORgtJoe Developerlt/AUTHORgt
lt/BOOKgt lt/XMLgt ltXML idXMLID
srcmydocument.xmlgt
51The XML Alphabet Soup Data Islands
- Can be embedded in an HTML SCRIPT element
- XML is accessible via the DOM
ltSCRIPT languagexml idXMLIDgt ltSCRIPT
typetext/xml idXMLIDgt ltSCRIPT
languagexml idXMLID srcmydocument.xmlgt
52The XML Alphabet Soup Data Islands
- Access the XML via the HTML DOM
- Or access the XML directly via the ID
function returnXMLData() return
document.all("XMLID").XMLDocument.nodeValue
function returnXMLData() return
XMLID.documentElement.text
53The XML Alphabet Soup Data Binding
- Client-side data binding (in the browser)
- The XML Data Source Object (DSO) binds HTML
elements to an XML data set (or data island) - When the XML data set changes, the bound elements
are updated dynamically - DATASRC the source of the data (e.g., the ID of
the data island) - DATAFLD the field (XML element) to display
- Can offload XSLT processing to the client
- IE only
54The XML Alphabet Soup Data Binding
ltHTMLgtltBODYgt ltXML ID"xmlParts"gt lt?xml
version"1.0" ?gt ltpartsgt ltpartgt
ltpartnumbergtA1000lt/partnumbergt
ltdescriptiongtFlat washerlt/descriptiongt
ltquantitygt1000lt/quantitygt lt/partgt
... lt/XMLgt lttable datasrcxmlParts
border1gtlttrgt lttdgtltdiv datafld"partnumber"gtlt/
divgtlt/tdgt lttdgtltdiv datafld"quantity"gtlt/divgtlt/
tdgt lt/trgtlt/tablegt lt/BODYgtlt/HTMLgt
55The XML Alphabet Soup XLang (BizTalk)
- An XML language for defining processes
- Processes usually on multiple platforms
- Support for
- Concurrency
- Long-running transactions
- Exposed messaging and orchestration APIs
- Connect to COM components
- Connect to MSMQ queues
- Connect to SQL components
56The XML Alphabet Soup XML-Based Applications
- Microsoft BizTalk Server
- Enables uniform exchange of data among disparate
systems (XLang) - Backend interchange with partners/customers
- Microsoft Commerce Server
- XML-based Product Catalog System
- Integrated with BizTalk Server for backend
communication
57The XML Alphabet Soup XML-Based Applications
- Microsoft SQL Server
- Retrieve relational data as XML
- Query XML data
- Join XML data with existing database tables
- Update the database via XML Updategrams
- Microsoft Exchange Server
- XML is native representation of many types of
data - Used to enhance performance of UI scenarios (for
example, Outlook Web Access (OWA))
58Agenda
- Overview
- Syntax and Structure
- The XML Alphabet Soup
- XML in .NET
- Relational Data and XML
59XML in .NETOverview
- XML use is ubiquitous throughout .NET
- Web Services
- Based on XML standards SOAP, WSDL, UDDI,
- ASP.NET
- Application information stored in XML-based
configuration files - ADO.NET
- Provides conversion between DataSets and XML
- DataSets are serialized as XML
60XML in .NETOverview
- XML classes built on industry standards
- e.g., DOM Level 2 Core, XML Schemas, SOAP
- Introduces a stream-based (pull) interface
- vs. the traditional SAX push model
- Natural evolution of MSXML 3.0 (4.0)
- MSXML 3.0 still available in .NET via COM Interop
- .NET XML classes are well-factored for
functionality, performance and extensibility
61XML in .NETCore Classes in System.XML
Abstract (Base) Class Concrete (Derived) Class
XmlNode XmlDocument XmlLinkedNode XmlElement XmlAttribute XmlDataDocument
XmlReader XmlTextReader XmlNodeReader
XmlWriter XmlTextWriter
62XML in .NET XmlNode
- Represents a single node in a XML document
hierarchy - An abstract class
- Properties and methods to
- traverse XML document hierarchy
- query properties
- modify nodes
- delete notes
63XML in .NET XmlDocument
- Implements W3C XML Document Object Model (DOM)
Level 1 and Level 2 specifications - Implements XmlNode
- Represents an entire XML document
- All nodes are available to view/manipulate
- Cached in memory
- XmlDocument is analogous to a DataSet
- Events for changing, inserting and removing nodes
64XML in .NET XmlDocument
- Declaration
- Constructor
- Code example
public class XmlDocument XmlNode
public XmlDocument()
XmlDocument myXmlDoc new XmlDocument() myXmlDoc
.Load(c\Sample.xml)
65XML in .NET XmlLinkedNode
- Implements XmlNode
- Retrieves the node immediately preceding or
following the current node - An abstract class from which XmlElement is
derived - Declaration
public abstract class XmlLinkedNode XmlNode
66XML in .NET XmlElement
- Represents an element in the DOM tree
- Properties and methods to view, modify, and
create element objects - Declaration
- Code example
public class XmlElement XmlLinkedNode
XmlDocument myXmlDoc new XmlDocument() myXmlDoc
.Load (c\Sample.xml) // DocumentElement
retrieves the root element XmlElement root
myXmlDoc.DocumentElement
67XML in .NET XmlAttribute
- Implements XmlNode
- Represents an attribute of an XmlElement
- Valid and/or default values defined by schema
- Declaration
public class XmlAttribute XmlNode
68XML in .NET XmlAttribute
XmlDocument myXmlDoc new XmlDocument() myXmlDoc
.Load (c\Sample.xml) // Get the attribute
collection of the root element XmlAttributeCollect
ion attrColl myXmlDoc.DocumentElement.Attributes
// Create a new attribute and set its
value XmlAttribute newAttr myXmlDoc.CreateAttrib
ute(value) newAttr.Value new value //
Append the new attribute to the
collection attrColl.Append(newAttr)
69XML in .NET Demo XmlDocument
70XML in .NETXmlNodeIXPathNavigable
- XmlNode has simple methods for traversing a XML
document hierarchy - XmlNode is an interface for accessing XML data
using a cursor model - Provides a generic navigation mechanism
- Support for XPath expressions through
IXPathNavigable interface - Can select and navigate a subset of a document
- Properties and methods to view, modify, copy,
delete nodes
71XML in .NET XPathNavigator
- Allows navigation over a XmlDocument
- XmlDocument is derived from XmlNode, which
implements IXPathNavigable - Caveats
- 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 XPathNavigator where it was
public class XmlDocument XMLNode XMLNode
IXPathNavigable.CreateNavigator()
72XML in .NET XPathNavigator
- Declaration
- Constructor
- Code example
public class XPathNavigator IXPathNavigable
public XPathNavigator (XmlDocument document)
XmlDocument myXmlDoc myXmlDoc.Load
(c\Sample.xml) XPathNavigator nav
myXmlDoc.CreateNavigator() nav.MoveToRoot()
// move to root element nav.MoveToNext() //
move to next element
73XML in .NET XPathNavigator
- Example iterate over a subset of nodes
XmlDocument doc new XmlDocument() doc.Load("per
son.xml") XPathNavigator nav
doc.CreateNavigator() XPathNodeIterator iter
nav.Select("/person/name") while
(iter.MoveToNext ()) // process selection
here with iter.Current.Value
- Example sum all Prices in a document
public static void SumPriceNodes(XPathNavigator
nav) // in this case, evaluate returns a
number Console.WriteLine("sum"
nav.Evaluate("sum(//Price)"))
74XML in .NET Demo XPathNavigator
75XML in .NET XmlReader
- XmlReader is an abstract class that provides
fast, non-cached, forward-only, read-only access
to XML data - Uses a pull model
- Simpler than the SAX push model
- User can easily implement push model if desired
- But the reverse is not true
- Reads in depth-first order
- Same order as textual XML data
- Analogous to an ADO.NET DataReader
76XML in .NET XmlTextReader
- Forward-only, read-only, non-cached access to
stream-based XML data - Implements XmlReader
- Access to data by parsing text input from
- Streams
- TextReader objects
- Strings
- Properties and methods to view elements and
attributes - Event support for validation
77XML in .NET XmlTextReader
- Declaration
- Constructor (overloaded)
- Code example
public class XmlTextReader XmlReader
public XmlTextReader(TextReader
textreader) public XmlTextReader(string
url) public XmlTextReader(Stream stream)
XmlTextReader reader new XmlTextReader(c\Sampl
e.xml) While (reader.Read()) ...
78XML in .NET XmlNodeReader
- Forward-only, non-cached access to XML data
- Implements XmlReader
- Access to data via XPathNavigator
- Reads data from an entire XmlDocument or a
specific XmlNode - Properties and methods to view elements and
attributes
79XML in .NET XmlWriter
- Abstract class providing fast, non-cached,
forward-only, write-only creation of XML data - Makes it easy to create well-formed XML data in a
type-safe manner
80XML in .NET XmlTextWriter
- Forward-only, non-cached method of writing XML
data to a stream - Implements XmlWriter
- Can write to
- Streams
- Files
- TextWriter objects
- Properties and methods to write syntactically
valid XML
81XML in .NET XmlTextWriter
- Declaration
- Constructor (overloaded)
- Code example
public class XmlTextWriter XmlWriter
public XmlTextWriter(TextWriter
textwriter) public XmlTextReader(string file,
Encoding encoding) public XmlTextWriter(Stream
stream, Encoding encoding)
XmlTextWriter writer new XmlTextWriter
(c\Sample.xml, null) writer.WriteStartDocument
() // Write the declaration writer.WriteStar
tElement(ROOT) // Write the root
element writer.WriteEndElement() //
Write the close tag of root writer.Flush()
// Writer XML to file writer.Close() //
Close writer
82XML in .NET Demo XmlTextReader XmlTextWriter
83XML in .NET Other XML Namespaces
- System.Xml Core XML namespace
- System.Xml.XPath contains the XPathNavigator,
XPath parser and evaluation engine - System.Xml.Xsl support XSLT transformations
- System.Xml.Serialization Classes to serialize
objects into XML documents or streams - System.Xml.Schema Supports XSD schemas
84Agenda
- Overview
- Syntax and Structure
- The XML Alphabet Soup
- XML in .NET
- Relational Data and XML
85Relational Data and XMLRelational Support in
System.Xml
- XmlDataDocument
- XPathDocument
- XPathNavigator
- Build a relation on a DataSet
- Built-in XML support in SQL Server 2000
86Relational Data and XML XmlDataDocument
- Extends XmlDocument to include support for
relational data (files, RDBMS, etc.) - Provides a DataSet property, which is kept
synchronized with the XML data - Schema support
- Schema can be loaded/saved as XSD
- Schema can be inferred from XML data
87Relational Data and XML XmlDataDocument
DataSet
88Relational Data and XML XmlDataDocument
XmlDataDocument dataDoc new XmlDataDocument()
// Read the XML into a DataSet dataDoc.DataSet.Rea
dXml(new StreamReader(c\example.xml) foreach(
DataTable table in dataDoc.DataSet.Tables)
foreach(DataColumn column in table.Columns)
foreach(DataRow row in table.Rows)
foreach(Object value in row.ItemArray)
89Relational Data and XML DataDocumentNavigator
- Implements XmlNavigator over an XmlDataDocument
- Contains the features of DocumentNavigator
- Cursor model access
- XPath support, etc.
- Includes support for relational data
(XmlDataDocument)
90Relational Data and XML Building a DataSet
Relation
- Create DataSet
- Define tables
DataSet dataset new DataSet() dataset.Name
"BookAuthors" DataTable authors new
DataTable("Author") DataTable books new
DataTable("Book")
91Relational Data and XML Building a DataSet
Relation
- Define columns
- Define keys
DataColumn id authors.Columns.Add("ID",
typeof(Int32)) id.AutoIncrement
true authors.PrimaryKey new DataColumn
id DataColumn name new authors.Columns.Add("N
ame", typeof(String)) DataColumn isbn
books.Columns.Add("ISBN", typeof(String)) books.P
rimaryKey new DataColumn isbn DataColumn
title books.Columns.Add("Title",
typeof(String)) DataColumn authid
books.Columns.Add(AuthID, typeof(Int32)) DataCo
lumn foreignkey new DataColumn authid
92Relational Data and XML Building a DataSet
Relation
- Add the tables to the DataSet
dataset.Tables.Add (authors) dataset.Tables.Add
(books)
93Relational Data and XML Building a DataSet
Relation
- Add data and save the DataSet
DataRow shkspr authors.NewRow() shkspr"Name"
"William Shakespeare" authors.Rows.Add(shkspr)
DataRelation bookauth new DataRelation("BookAu
thors", authors.PrimaryKey,
foreignkey) dataset.Relations.Add
(bookauth) DataRow row books.NewRow() row"Au
thID" shkspr"ID" row"ISBN"
"1000-XYZ" row"Title" "MacBeth" books.Rows.A
dd(row) dataset.AcceptChanges()
94Relational Data and XML XML Support in SQL
Server 2000
- 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
95Conclusion
- Overview
- Syntax and Structure
- The XML Alphabet Soup
- XML in .NET
- Relational Data and XML
96Resources
- http//msdn.microsoft.com/xml/
- http//www.xml.com/
- http//www.w3.org/xml/
- Microsoft Press Books (http//mspress.microsoft.co
m/) - XML Step By Step
- XML In Action
- Developing XML Solutions
- OReilly Press
- XML In A Nutshell
97Resources
- XML in .NEThttp//msdn.microsoft.com/msdnmag/issu
es/01/01/xml/xml.asp - Working with XML in the .NET Platformhttp//www.x
mlmag.com/upload/free/features/xml/2001/05may01/dw
0102/dw0102.asp