Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Learning 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
Agenda
Overview
Syntax and Structure
The XML Alphabet Soup
XML in .NET
Relational Data and XML
Overview
What is XML?
XML XML
Application X
Repository Database
Overview
Documents vs. Data
Submissions by
Microsoft
IBM
Hewlett-Packard
Fujitsu Laboratories
Sun Microsystems
Netscape (AOL), and others…
Technologies using XML
SOAP, ebXML, BizTalk, WebSphere, many others…
Agenda
Overview
Syntax and Structure
The XML Alphabet Soup
XML in .NET
Relational Data and XML
Syntax and Structure
Components of an XML Document
Elements
Each element has a beginning and ending tag
<TAG_NAME>...</TAG_NAME>
Elements can be empty (<TAG_NAME />)
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
Syntax and Structure
Components of an XML Document
<?xml version=“1.0” ?>
<?xml-stylesheet type="text/xsl" href=“template.xsl"?>
<ROOT>
<ELEMENT1><SUBELEMENT1 /><SUBELEMENT2 /></ELEMENT1>
<ELEMENT2> </ELEMENT2>
<ELEMENT3 type=‘string’> </ELEMENT3>
<ELEMENT4 type=‘integer’ value=‘9.3’> </ELEMENT4>
</ROOT>
Yes
xmlns: bk = “urn:mybookstuff.org:bookinfo”
xmlns: bk = “http://www.example.com/bookinfo/”
<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
<BOOK xmlns=“http://www.bookstuff.org/bookinfo”>
<TITLE>All About XML</TITLE>
<AUTHOR>Joe Developer</AUTHOR>
Syntax and Structure
Namespaces: Scope
XML document:
<BOOK>
<TITLE>All About XML</TITLE>
<AUTHOR>Joe Developer</AUTHOR>
</BOOK>
DTD schema:
<!DOCTYPE BOOK [
<!ELEMENT BOOK (TITLE+, AUTHOR) >
<!ELEMENT TITLE (#PCDATA) >
<!ELEMENT AUTHOR (#PCDATA) >
]>
The XML ‘Alphabet Soup’
Schemas: XSD Example
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="NewDataSet“ targetNamespace="http://tempuri.org/schema1.xsd"
xmlns="http://tempuri.org/schema1.xsd"
xmlns:xsd="http://www.w3.org/1999/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:element name="book">
<xsd:complexType content="elementOnly">
<xsd:all>
<xsd:element name="title" minOccurs="0" type="xsd:string"/>
<xsd:element name="author" minOccurs="0" type="xsd:string"/>
</xsd:all>
</xsd:complexType>
</xsd:element>
<xsd:element name=“Catalog" msdata:IsDataSet="True">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element ref="book"/>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
The 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
The XML ‘Alphabet Soup’
Transformations: XSL
XML
XML,
HTML,
…
XSLT
The XML ‘Alphabet Soup’
Transformations: XSLT
/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
<XML id=“XMLID”>
<BOOK>
<TITLE>All About XML</TITLE>
<AUTHOR>Joe Developer</AUTHOR>
</BOOK>
</XML>
Declaration:
public class XmlDocument : XmlNode
Constructor:
public XmlDocument();
Code example:
XmlDocument myXmlDoc = new XmlDocument();
myXmlDoc.Load(“c:\Sample.xml”);
XML 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
XML in .NET
XmlElement
Implements XmlNode
Represents an attribute of an XmlElement
Valid and/or default values defined by schema
Declaration:
public class XmlAttribute : XmlNode
XML in .NET
XmlAttribute
Code example:
XmlDocument myXmlDoc = new XmlDocument();
myXmlDoc.Load (“c:\Sample.xml”);
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
XML in .NET
XPathNavigator
Declaration:
public class XPathNavigator : IXPathNavigable
Constructor:
public XPathNavigator (XmlDocument document);
Code example:
XmlDocument myXmlDoc;
myXmlDoc.Load (“c:\Sample.xml”);
Constructor (overloaded):
public XmlTextReader(TextReader textreader);
public XmlTextReader(string url);
public XmlTextReader(Stream stream);
Code example:
XmlTextReader reader = new XmlTextReader(“c:\Sample.xml”);
While (reader.Read()) {
...
}
XML in .NET
XmlNodeReader
Constructor (overloaded):
public XmlTextWriter(TextWriter textwriter);
public XmlTextReader(string file, Encoding encoding);
public XmlTextWriter(Stream stream, Encoding encoding);
Code example:
XmlTextWriter writer = new XmlTextWriter (“c:\Sample.xml”, null);
writer.WriteStartDocument(); // Write the declaration
writer.WriteStartElement(“ROOT”); // Write the root element
writer.WriteEndElement(); // Write the close tag of root
writer.Flush(); // Writer XML to file
writer.Close(); // Close writer
XML in .NET
Demo XmlTextReader & XmlTextWriter
XML 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
Agenda
Overview
Syntax and Structure
The XML Alphabet Soup
XML in .NET
Relational Data and XML
Relational Data and XML
Relational Support in System.Xml
XmlDataDocument
XPathDocument
XPathNavigator
Build a relation on a DataSet
Built-in XML support in SQL Server 2000
Relational Data and XML
XmlDataDocument
XmlData- DataSet
Document Sync
Create DataSet
Define tables:
DataSet dataset = new DataSet();
dataset.Name = "BookAuthors";
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("Name", typeof(String));
authors.PrimaryKey, foreignkey);
dataset.Relations.Add (bookauth);
dataset.AcceptChanges();
Relational Data and XML
XML Support in SQL Server 2000