Title: Administrivia
1Administrivia
- Quiz on Tuesday
- Focus on lectures 1 through 4
- General notions about .NET
- C features
- C in hw1
- XNA
- Discussions
- Short answers, T/F, Multiple choice
- Closed book
- 20 to 30 minutes max
- Any questions?
2Goal
- We may or may not need to use XML this semester,
but learning it is important, so here you go!!! - XML
- What it is and how to use it
- Some parts again stolen from
- Mark Sapossnek, CS Dept, Metropolitan College,
Boston University, CS 594 (in MSDNAA Curriculum
Repository)
3XML Overview
- Extensible Markup Language (XML)
- Derived from Standard Generalized Markup Language
(SGML) - HTML is also derived from SGML
- Plain-text, Unicode-based meta-language
- A language for defining markup languages using
markup tags (words surrounded by lt and gt) - Generic
- Not programming language specific
- Not operating system specific
- Not software vendor specific
4XML Overview - 2
- XML provides
- Manipulating data
- Structuring data
- Transforming data
- Querying data
- Originally for documents, now for any structured
data - Lingua franca of information interchange
5More on XML
- Extensible
- Unlike HTML which has a fixed vocabulary, XML can
be used to define app dependent vocabularies - Additive changes to XML documents allow old code
to still function - Text-based
- Allows using standard tools to edit if the need
arises - Platform independence
- Interoperability is a natural consequence
- Strict rules
- Syntax
- Structure
- Case sensitive
- Separates presentation from content
- Permits reuse of content information
6Syntax and Structure
- Prolog contains processing instructions
- Encoding specification (Unicode by default)
- Namespace declaration
- Schema declaration
- 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
7Syntax and Structure - 2
- 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)
8Well-Formed XML Rules
- There must be one, and only one, root element
- Sub-elements must be properly nested
- A tag must end with 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
9Well-Formed XML? - 1
- No, CHILD2 and CHILD3 do not nest propertly
ltxml? Version1.0 ?gt ltPARENTgt ltCHILD1gtThis is
element 1lt/CHILD1gt ltCHILD2gtltCHILD3gtNumber
3lt/CHILD2gtlt/CHILD3gt lt/PARENTgt
10Well-Formed XML? - 2
- 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
11Well-Formed XML? - 3
lt?xml Version1.0 ?gt ltPARENTgt ltCHILD1gtThis is
element 1lt/CHILD1gt ltCHILD2/gt ltCHILD3gtlt/CHILD3gt lt
/PARENTgt
12Namespaces 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 - Essentially same concept as namespace in C, C,
- Identified by a URI (Uniform Resource Identifier)
- When a URI is used, it does NOT have to represent
a live server - Understand what that means? Must be a unique
string!!
13Sample
- lt?xml version"1.0" encoding"iso-8859-1" ?gt
- lt?xml-stylesheet href"orders.xsl"?gt
- ltorder id"ord123456"gt
- ltcustomer id"cust0921"gt
- ltfirst-namegtDarelt/first-namegt
- ltlast-namegtObasanjolt/last-namegt
- ltaddressgt
- ltstreetgtOne Microsoft Waylt/streetgt
- ltcitygtRedmondlt/citygt
- ltstategtWAlt/stategt
- ltzipgt98052lt/zipgt
- lt/addressgt
- lt/customergt
- ltitemsgt
- ltcompact-discgt
- ltpricegt16.95lt/pricegt
- ltartistgtNellylt/artistgt
- lttitlegtNellyvillelt/titlegt
14Sample continued
Namespace
-
- lt!-- Always go the extra mile for the customer
--gt - ltspecial-instructions xmlnshtml"http//www.w3.
org/1999/xhtml/"gt - lthtmlpgtIf customer is not available at the
- address then attempt leave package at one
of - the following locations listed in order of
- which should be attempted first
- lthtmlolgt
- lthtmlligtNext Doorlt/htmlligt
- lthtmlligtFront Desklt/htmlligt
- lthtmlligtOn Doorsteplt/htmlligt
- lt/htmlolgt
- lthtmlbgtNotelt/htmlbgt Remember to leave a
note - detailing where to pick up the package.
- lt/htmlpgt
- lt/special-instructionsgt
- lt/ordergt
Namespace reference
15XML Schema
- Defines the structure of the XML data
- ltxsschema xmlnsxs"http//www.w3.org/2001/XMLSch
ema"gt - ltxselement name"items"gt
- ltxscomplexTypegt
- ltxssequencegt
- ltxselement ref"compact-disc"
minOccurs"0" maxOccurs"unbounded" /gt - lt/xssequencegt
- lt/xscomplexTypegt
- lt/xselementgt
- ltxselement name"compact-disc"gt
- ltxscomplexTypegt
- ltxssequencegt
- ltxselement name"price" type"xsdecimal"
/gt - ltxselement name"artist" type"xsstring"
/gt
16Writing an XML Document
- System.Xml.XmlTextWriter
- One constructor takes a file name
- XmlTextWriter w new XmlTextWriter(foo.xml)
- Another takes a stream (which is useful for
creating a string) - using System.IO using System.Xml
- StringWriter sw new StringWriter()
- XmlTextWriter w new XmlTextWriter(sw)
- WriteStartElement (and WriteEndElement)
- WriteAttributeString
- w.WriteStartElement(order)
- w.WriteAttributeString(id, 33343)
- embed other write calls
- w.WriteEndElement()
17Access to an XML Document
- Tree view
- Memory resident, tree of nodes
- Document Object Model (DOM)
- XmlDocument class
- Cursor view
- Not required to be in memory
- Cursor moves over document allowing examination
of the contents - XPathNavigator class
- Streaming API
- Manipulate as a stream for transformational and
other processing - Push-based model SAX pushes events to
handlers - Pull-based model forward-only cursor over data
(XmlReader class)
18Reading XML
- XmlTextReader
- Again from a stream or file
- Check out NodeType to determine what you have
- XmlNodeType.Element, .Text, .Attribute
System.IO.StringReader sr new
System.IO.StringReader("ltcompute op'
arg1'32' arg2'44'/gt") System.Xml.XmlTextReader
tr new System.Xml.XmlTextReader(sr) tr.Whitespa
ceHandling WhitespaceHandling.None tr.Read() C
onsole.WriteLine("Ele 01", tr.Name,
tr.Value) // Ele compute tr.MoveToAttribute("ar
g1") int arg1 Convert.ToInt32(tr.Value) Consol
e.WriteLine("Attr 01", tr.Name, tr.Value)
// Attr arg132 tr.MoveToAttribute("arg2") int
arg2 Convert.ToInt32(tr.Value) Console.WriteLin
e("Attr 01", tr.Name, tr.Value) // Attr
arg244 tr.MoveToAttribute("op") string op
tr.Value Console.WriteLine("Attr 01",
tr.Name, tr.Value) // Attr op
19Other Tools
- XML Query
- Ability to find out information about the
contents of an XML document - XPath and XQuery
- XML Transformation
- Take XML documents and munge them into new
documents - XSLT is the prime contender for XML
transformation - XHTML
- XML version of HTML (eliminates some of the odd
features of HTML) - Such as the ltpgt tag without closing tag
- Most browsers accept XHTML just fine
20XSLT Sample
- ltxslstylesheet xmlnsxsl"http//www.w3.org/1999/
XSL/Transform" version"1.0" xmlns"http//www.w3.
org/1999/xhtml"gt - ltxsloutput method"xml" indent"yes"
- doctype-system"http//www.w3.org/TR/xhtml1/DT
D/xhtml1-transitional.dtd" - doctype-public"-//W3C//DTD XHTML 1.0
Transitional//EN" /gt - ltxsltemplate match"/"gt
- lthtml lang"en" xmllang"en"gt
- ltheadgt
- lttitlegtOrder Information -
ord123456lt/titlegt - lt/headgt
- ltbodygt
- lttable border"1"gt
- lttrgtltthgtArtistlt/thgtltthgtTitlelt/thgtltthgtPrice
lt/thgtlt/trgt - ltxslfor-each select"items/compact-disc"gt
- lttrgt
- lttdgtltxslvalue-of xmlns""
select"artist" /gtlt/tdgt - lttdgtltxslvalue-of xmlns"" select"title"
/gtlt/tdgt - lttdgtltxslvalue-of xmlns"" select"price"
/gtlt/tdgt - lt/trgt
- lt/xslfor-eachgt
21Produces This XHTML
- lt!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN" "http//www.w3.org/TR/xhtml1/DTD
/xhtml1-transitional.dtd"gt - lthtml lang"en" xmllang"en" xmlns"http//www.w3
.org/1999/xhtml"gt - ltheadgt
- lttitlegtOrder Information - ord123456lt/titlegt
- lt/headgt
- ltbodygt
- lttable border"1"gt
- lttrgt
- ltthgtArtistlt/thgt
- ltthgtTitlelt/thgt
- ltthgtPricelt/thgt
- lt/trgt
- lttrgt
- lttdgtNellylt/tdgt
- lttdgtNellyvillelt/tdgt
- lttdgt16.95lt/tdgt
- lt/trgt
- lttrgt
- lttdgtBaby Dlt/tdgt
And this table
22Serialization
- Allows you to create a class and turn its
contents into XML using XMLSerializer - Public class bar public int mumble
- XMLSerializer foo new XMLSerializer(typeof(bar))
- StringWriter swF new StringWriter()
- foo.Serialize(swF, b) // b is an instance of
class bar - Console.WriteLine(swF.ToString())
- // produces
- lt?xml version"1.0" encoding"utf-16"?gt
- ltbargt
- ltmumblegt123lt/mumblegt
- lt/bargt
23DeSerialization
- Deserializer member function of XMLSerializer
allows you to take an XML and turn it back into
an object - StringReader srF new StringReader(swF.ToString()
) - bar b2 (bar)foo.Deserialize(srF)
- Console.WriteLine(bar.mumble)
- Can control XML generation with attributes
- THIS IS COOL!!!
24XML Summary
- XML is THE thing for sending data around
- Text-based
- Structured
- Standardized
- Many tools and standards for manipulation
- Easy to program with .NET Framework