Programming XML in the Microsoft 'NET Framework Part I Karthik Ravindran Karthi Karthikeyan PSS XML - PowerPoint PPT Presentation

1 / 56
About This Presentation
Title:

Programming XML in the Microsoft 'NET Framework Part I Karthik Ravindran Karthi Karthikeyan PSS XML

Description:

Examine DOM parsing in the .NET Framework ... Memory intensive loading very large XML documents into the DOM can deplete system resources ... – PowerPoint PPT presentation

Number of Views:281
Avg rating:3.0/5.0
Slides: 57
Provided by: MicrosoftC
Category:

less

Transcript and Presenter's Notes

Title: Programming XML in the Microsoft 'NET Framework Part I Karthik Ravindran Karthi Karthikeyan PSS XML


1
Programming XML in the Microsoft .NET Framework
Part I Karthik Ravindran Karthi
KarthikeyanPSS XML Web Database Microsoft
Corporation
2
Agenda
  • Introduce the core XML related Microsoft .NET
    Framework namespaces
  • Introduce the XML parsing models in the .NET
    Framework
  • Examine DOM parsing in the .NET Framework
  • Examine the implementation of pull model XML
    parsing in the .NET Framework
  • Examine the process of programmatically
    generating XML in the .NET Framework

3
Agenda (2)
  • Examine the process of programmatically
    validating XML documents
  • Examine the process of programmatically executing
    XSLT transformations
  • Examine the process of programmatically executing
    XPath queries
  • Q/A

4
A Birds Eye View of the .NET Framework
ASP.NET Applications
ASP.NET Web Applications
Windows Forms Applications
ASP.NET Web Services
Windows Services
.NET Framework Base classes
XML
ADO.NET
SECURITY
NETWORK
SECURITY
MESSAGING
WINFORMS
WEB
ETC.,
Common Language Runtime
JIT Compilers
Memory Management
Object Life cycle Management
Common Type System
Operating System
MACHINE
5
The Core .NET Framework XML Namespaces
  • System.Xml
  • The overall namespace for the .NET Framework
    classes provide standards-based support for
    parsing XML
  • Supported W3C XML standards
  • XML 1.0 and XML namespaces
  • XML schemas
  • XPath
  • XSLT
  • DOM level 2 core
  • SOAP 1.1 (used in object serialization)

6
The Core .NET Framework XML Namespaces (2)
  • System.Xml.Xsl
  • Contains classes that provide support for XSLT
    transformations
  • System.Xml.XPath
  • Contains classes that provide support for
    executing XPath queries
  • System.Xml.Schema
  • Contains classes that provide standards-based
    support for W3C XML schemas
  • System.Xml.Serialization
  • Contains classes that are used to serialize and
    deserialize .NET Framework objects to and from XML

7
XML Parsing Models
  • Commonly known XML parsing models
  • The DOM model
  • Forward only push model parsing (SAX)
  • Forward only pull model parsing
  • The .NET Framework XML parsing models
  • Forward only pull model parsing
  • DOM Parsing
  • Advantages and limitations of each model

8
Programming the XML DOM Using the System.Xml DOM
Classes
9
The DOM Parsing Model
  • In memory XML parsing
  • A tree structure is created in memory to
    represent the contents of the XML document being
    parsed
  • The parsing model of choice when there is a need
    to dynamically navigate through and manipulate
    (insert, update, and delete) the contents of an
    XML document
  • Not a good choice when the only requirement is to
    parse an XML document from top to bottom in a
    read-only fashion
  • Memory intensive loading very large XML
    documents into the DOM can deplete system
    resources

10
The Key System.Xml DOM Classes (Inheritance
Hierarchy)
Object
XmlNode XmlLinkedNode XmlCharacterData XmlDec
laration XmlDocumentType XmlElement XmlEntit
yRef XmlProcessingInstruction XmlAttribute Xml
Document XmlDocumentFragment XmlDocumentType Xm
lEntity XmlNotation XmlNamedNodeMap XmlAttribute
Collection XmlNodeList
XmlCDATASection XmlComment XmlSignificantWhiteSpac
e XmlWhiteSpace
11
The Key System.Xml DOM Classes(Aggregation
Hierarchy)
XmlDocument
XmlNodeList
XmlNode XmlLinkedNode XmlCharacterData XmlDec
laration XmlDocumentType XmlElement XmlEntit
yRef XmlProcessingInstruction XmlNamedNodeMap
XmlDocumentType XmlEntity XmlNotation
XmlCDATASection XmlComment XmlSignificantWhiteSpac
e XmlWhiteSpace
XmlAttributeCollection
XmlAttribute
12
Common Steps When Parsing an XML Document Using
the DOM
  • Execute the Load/LoadXml method of an XmlDocument
    object to load the XML data into a DOM tree in
    memory
  • Access/insert/update/delete nodes in the DOM tree
    structure using the methods and properties of the
    various type-specific DOM node objects
  • Execute the Save method of the XmlDocument object
    to persist the contents of the DOM to a file on
    disk, or a stream

13
Code Sample DOM Parsing in System.Xml (Sample
XML Document)
  • lt?xml version"1.0"?gt
  • ltBooksgt
  • ltBook ISBN"0355605172"/gt
  • ltTitlegtBeginning XMLlt/Titlegt
  • ltPricegt40.00lt/Pricegt
  • lt/Bookgt
  • ltBook ISBN"0415205173"/gt
  • ltTitlegtXML Step by Steplt/Titlegt
  • ltPricegt50.00lt/Pricegt
  • lt/Bookgt
  • lt/Booksgt

14
DOM Code Sample Accessing and Modifying Element
Data
  • Dim xmldoc As New XmlDocument()
  • xmldoc.Load("c\books.xml")
  • Dim PriceNodes As XmlNodeList
  • Dim PriceNode As XmlNode
  • Dim Price As Double
  • PriceNodes xmldoc.GetElementsByTagName("Price")
  • For Each PriceNode In PriceNodes
  • Price CType(PriceNode.InnerText,
    Double)
  • If Price gt 50 Then
  • Price Price - ((5 / 100)
    Price)
  • PriceNode.InnerText
    CType(Price, String)
  • End If
  • Next
  • xmldoc.Save("c\books.xml")

15
DOM Code Sample Accessing and Adding Attributes
  • Dim xmldoc As New XmlDocument()
  • xmldoc.Load("c\books.xml")
  • Dim BookNodes As XmlNodeList
  • Dim BookNode As XmlNode
  • Dim BookAttList As XmlNamedNodeMap
  • Dim qohAttribute As XmlAttribute
  • BookNodes xmldoc.GetElementsByTagName("Book")
  • For Each BookNode In BookNodes
  • BookAttList BookNode.Attributes
  • qohAttribute xmldoc.CreateAttribute(
    "qoh")
  • qohAttribute.InnerText ""
  • BookAttList.SetNamedItem(qohAttribute)
  • Next
  • xmldoc.Save("c\books.xml")

16
Implementing Non-Cached Pull Model XML Parsing in
the .NET Framework
17
Non-Cached Parsing
  • XmlReader and XmlWriter are the abstract classes
    that implement the API for reading and writing.
  • The XmlReader is a pull model parser. The pull
    model has several advantages over the SAX push
    model state management, selective processing,
    layering, etc.
  • Concrete implementation
  • XmlTextReader - provides non-cached,
    forward-only, read-only access
  • XmlTextWriter - provides a fast non-cached
    forward-only way of generating XML data

18
XmlTextReader Class
  • Fast and efficient way of parsing XML data
  • Enforces well-formedness does not provide data
    validation
  • Has some properties that can be modified while
    reading WhitespaceHandling, XmlResolver, etc.
  • Provides ways to skip content (improves
    efficiency)
  • MoveToContent method moves directly to content
    node and skips node types such as
    ProcessingInstruction, Comment, Attribute,
    Whitespace, etc.
  • Skip method skips child nodes of current node

19
XmlTextReader Class
  • Can read data from several input formats such as
    File, Stream, and TextReader
  • Key members
  • Methods that begin with MoveTo provide ways to
    navigate to the required attribute, element, or
    node
  • Methods that begin with Read
  • Provide ways to read different formats (Hex,
    Base64, etc.) and return decoded binary bytes
  • Provide ways to read content of XML data,
    attribute values, etc.
  • Properties Encoding, Depth, LineNumber,
    ReadState, Value, etc.

20
Code Sample Non-Cached Parsing
' A file, URL or Stream can be passed into the
constructor for the XmlTextReader Dim input As
String input "large.xml" Load the reader
with the data file and ignore all whitespace
nodes Dim reader As New XmlTextReader(input) reade
r.WhitespaceHandling WhitespaceHandling.None '
Move directly to content nodes, i.e., skips
whitespaces, processing instructions
reader.MoveToContent() Parse the file and
display each of the nodes. While
reader.Read() '..process here End While ' Close
the reader If Not (reader Is Nothing) Then
reader.Close() End If
21
Dynamically Generating XML by Programming the
.NET Framework XML Classes
22
Programmatically Generating XML in the .NET
Framework
  • Options available to programmatically generate
    XML
  • Non-cached, forward-only streaming
  • Programming the DOM
  • Advantages and limitations of each method

23
Introducing the XmlTextWriter
  • Implemented in the System.Xml .NET Framework
    namespace
  • Inherits from the System.Xml.XmlWriter abstract
    class
  • Used to programmatically generate XML in a
    non-cached, forward-only fashion
  • Can be used to generate XML to a file on disk and
    .NET Framework Stream/TextWriter objects

24
XmlTextWriter Constructors
  • Public Sub New(ByVal filename As String, ByVal
    encoding As Encoding)
  • Public Sub New(ByVal strm As Stream, ByVal
    encoding As Encoding)
  • Public Sub New(ByVal w As TextWriter)

25
Some Commonly Used Properties and Methods of the
XmlTextWriter
  • Properties
  • Formatting
  • Indentation
  • QuoteChar
  • IndentChar
  • WriteState
  • Methods
  • The WriteXXX methods (example
    WriteStartDocument, WriteStartElement,
    WriteAttributeString, etc.)
  • Close

26
Common Steps in Using the XmlTextWriter to
Generate XML
  • Instantiate an XmlTextWriter object using one of
    the listed constructors
  • Set the Formatting and Indentation properties to
    format the generated output
  • Execute the WriteXXX methods to generate the
    contents of the XML document
  • Execute the Close() method to close the target
    stream

27
Code Sample Using the XmlTextWriter (Sample XML
Document)
  • lt?xml version"1.0"?gt
  • lt!--Catalog fragment--gt
  • lt!DOCTYPE Books SYSTEM "books.dtd"gt
  • ltBooksgt
  • ltBook ISBN"0355605172"/gt
  • ltTitlegtXML Step by Steplt/Titlegt
  • lt/Bookgt
  • lt/Booksgt

28
Code Sample Using the XmlTextWriter to Generate
XML
  • Dim wrt As XmlTextWriter New
    XmlTextWriter("c\books.xml", Nothing)
  • wrt.Formatting System.Xml.Formatting.Ind
    ented
  • wrt.WriteStartDocument(False)
  • wrt.WriteComment("Catalog fragment")
  • wrt.WriteDocType("Books", Nothing,
    "books.dtd", Nothing)
  • wrt.WriteStartElement("Books")
  • wrt.WriteStartElement("Book")
  • wrt.WriteAttributeString("", "ISBN", "",
    "0355605172")
  • wrt.WriteStartElement("Title")
  • wrt.WriteString(XML Step by Step")
  • wrt.WriteEndElement()
  • wrt.WriteEndElement()
  • wrt.WriteEndElement()

29
Code Sample Using the DOM to Generate XML
  • Dim xmldoc As New XmlDocument()
  • Dim xmldecl As XmlDeclaration
  • Dim xmlComment As XmlComment
  • Dim docType As XmlDocumentType
  • Dim xmlfragment As XmlDocumentFragment
  • xmldecl xmldoc.CreateXmlDeclaration("1.0",
    Nothing, Nothing)
  • xmldoc.AppendChild(xmldecl)
  • docType xmldoc.CreateDocumentType("Books",
    Nothing, "c\books.dtd", Nothing)
  • xmldoc.AppendChild(docType)
  • xmlComment xmldoc.CreateComment("Catalog
    fragment")
  • xmldoc.AppendChild(xmlComment)
  • xmldoc.AppendChild(xmldoc.CreateElement("Books"))
  • xmldoc.DocumentElement.AppendChild(GenerateBookNod
    e(xmldoc, "XML Step by Step", "0355605172"))

30
Code Sample Using the DOM to generate XML (2)
  • Private Function GenerateBookNode(ByVal xmldoc As
    XmlDocument, ByVal Title As String, ByVal ISBN As
    String) As XmlNode
  • Dim BookNode As XmlNode
  • BookNode xmldoc.CreateElement("Book")
  • BookNode.AppendChild(xmldoc.CreateElement(
    "Title"))
  • BookNode.ChildNodes(0).InnerText Title
  • BookNode.Attributes.Append(xmldoc.CreateAt
    tribute("ISBN"))
  • BookNode.Attributes.GetNamedItem("ISBN").I
    nnerText ISBN
  • GenerateBookNode BookNode
  • End Function

31
Validating XML Documents in the .NET Framework
32
Validation of XML
  • Schemas help define the structure of XML
    documents. Validation ensures that the external
    data conforms to the rules (grammar) required by
    the schema.
  • The three language recommendations
  • Document Type Definitions (DTD)
  • XML Data Reduced schema (XDR)
  • XML Schema Definition language (XSD)
  • XSD is the future. Schemas have several
    advantages over DTD
  • Schemas use XML syntax and can be parsed by an
    XML parser.
  • Schemas offer data type support (integer, string,
    Boolean), and the ability to create other data
    types.

33
Schemas in .NET
  • XML data can be validated against all the three
    schema languages using the .NET classes.
    System.Xml.XmlValidatingReader is used for
    validation.
  • System.Xml.Schema is the namespace for the XML
    classes that provide standards-based support for
    XML schemas (for structures and data types).
  • System.Xml.Schema.XmlSchemaCollection Class
    contains a cache of XSD and XDR schemas.

34
XmlValidatingReader Class
  • Represents a reader that provides DTD, XDR or XSD
    schema validation
  • This class implements XmlReader
  • The parser does not stop for any kind of
    validation error and only stops if the data is
    not well formed
  • Key members
  • Schemas property Gets a XmlSchemaCollection to
    use for validation
  • ValidationType property Specifies what type of
    validation the reader should perform (auto, none,
    DTD, XDR, or schema)
  • ValidationEventHandler event Sets an event
    handler for receiving information about
    validation errors

35
XmlSchemaCollection Class
  • A collection for holding schemas. Used by the
    XmlValidatingReader class.
  • Schemas are loaded using the Add method, at which
    time the schema is associated with a namespace
    URI.
  • Current version supports the http//www.w3.org/200
    1/XMLSchema XSD recommendation.
  • Does not support caching DTDs.
  • Any method and property that takes or returns an
    XmlSchema applies only to XSD schemas.

36
Code Sample Validating an XML Document
  • Public Shared Sub Main()
  • ' Add the schema to a schemaCollection
    instance
  • Dim sc As XmlSchemaCollection New
    XmlSchemaCollection()
  • AddHandler sc.ValidationEventHandler,
    AddressOf ValidationCallBack
  • sc.Add(Nothing, "schema.xsd")
  • If (sc.Count gt 0) Then
  • ' Initialize the validating reader
  • Dim tr As XmlTextReader New
    XmlTextReader("booksSchemaFail.xml")
  • Dim rdr As XmlValidatingReader New
    XmlValidatingReader(tr)
  • ' Set the validation type to XSD
    Schema
  • rdr.ValidationType
    ValidationType.Schema
  • rdr.Schemas.Add(sc)
  • ' Add a validation event handler and
    read the data
  • AddHandler rdr.ValidationEventHandler,
    AddressOf ValidationCallBack
  • While (rdr.Read())
  • End While

37
Executing XSLT Transformations in the .NET
Framework
38
XSL Transformations
  • XSL (Extensible Stylesheet Language) consists of
    three parts
  • XSLT XSL transformations
  • XPath XML path language
  • XSL-FO XSL formatting objects
  • XSLT is a language for transforming XML documents
    into text-based documents
  • Transformation process involves three documents
  • Source XML document
  • Stylesheet document
  • Output document XML, HTML, etc.

39
XSLT in .NET
  • Implemented under System.Xml.Xsl namespace
  • Supports W3C XSLT 1.0 recommendation
  • Key classes
  • XslTransform Transforms XML data using an XSLT
    stylesheet
  • XsltArgumentList Allows parameters and
    extension objects to be invoked from within the
    stylesheet
  • XsltException Returns information about the
    last exception thrown while processing an XSL
    transform

40
XSLT Architecture in .NET
41
XslTransform Class
  • The XSLT stylesheet must include the namespace
    declaration "xmlnsxsl http//www.w3.org/1999/XSL
    /Transform"
  • Key methods
  • Load
  • Loads the XSLT stylesheet including any
    xslinclude and xslimport references
  • This overloaded method can load the stylesheet
    using an XmlReader, XPathNavigator, or a URL with
    the file location
  • Transform
  • Transforms the XML data and outputs the results
  • Has several overloads to handle different input
    and output formats such as Stream, File,
    XMLReader, etc.

42
Code Sample XslTransform
Transforms the XML data in the input file and
outputs the result to an output file Dim xslt As
XslTransform New XslTransform()
xslt.Load("books.xsl") xslt.Transform("books.x
ml", "output.xml") ' Transforms the XML data in
the XPathDocument and outputs the result to an
XmlReader Dim URL As String "http//samples.gotd
otnet.com/quickstart/howto/samples/Xml/TransformXm
l/VB/" Dim xslt As XslTransform New
XslTransform() xslt.Load(URL "books.xsl") Dim
doc As XPathDocument New XPathDocument(URL
"books.xml") Dim reader As XmlReader
xslt.Transform(doc, Nothing) Transforms the
XML data in the input file and outputs the result
to a stream Dim xslt As XslTransform New
XslTransform() xslt.Load("books.xsl") Dim strm
As New MemoryStream() xslt.Transform(New
XPathDocument("books.xml"), Nothing, strm)
43
XsltArgumentList Class
  • This class is used by the Transform method
  • When added, parameters and objects are associated
    with a namespace qualified name and a namespace
    URI respectively
  • Key methods
  • AddX, GetX, and RemoveX (where X Param or
    ExtenstionObject)
  • Methods used to add, get, and remove parameters
    or extension objects
  • The parameter should be one of the W3C XPath
    types if not, it is coerced into Double or
    String as appropriate
  • Clear
  • Removes all parameters and extension objects

44
Code Sample XsltArgumentList
'Transforms the XML data in the XmlDocument using
parameters and outputs the result to an Xml
Writer Dim xslt As XslTransform New
XslTransform() ' Load the stylesheet xslt.Load("o
rder.xsl") ' Create the XsltArgumentList and
pass current datetime as a parameter Dim xslArg
As XsltArgumentList New XsltArgumentList() Dim
d As DateTime DateTime.Now xslArg.AddParam("date
", "", d.ToString()) ' Load the XML data into
XmlDocument Dim doc As New XmlDocument() doc.Load(
"order.xml")
' Create the XmlTextWriter to write the output
to the console and tranform the data Dim writer
As XmlTextWriter New XmlTextWriter(Console.Out)
xslt.Transform(doc.CreateNavigator, xslArg,
writer)
45
Executing XPath Queries Using the .NET Framework
XML Classes
46
Overview of XPath
  • A query language for XML the SQL of XML
  • Used to specify query expressions to locate nodes
    in an XML document
  • Used in XSLT stylesheets to locate and apply
    transformations to specific nodes in an XML
    document
  • Used in DOM code to locate and process specific
    nodes in an XML document

47
XPath in the .NET Framework
  • Related namespaces
  • System.Xml
  • System.Xml.XPath
  • Key .NET Framework classes
  • XmlDocument, XmlNodeList, and XmlNode
  • XPathDocument
  • XPathNavigator
  • XPathNodeIterator
  • XPathExpression

48
Executing XPath Queries Using the System.Xml DOM
Objects
  • Commonly used classes XMLDocument, XMLNodeList,
    XMLNode
  • Load the XML document into the XMLDocument class
  • Use the selectNodes/selectSingleNode method of
    the XmlDocument class to execute the XPath query
  • Assign returned node list/node to an
    XmlNodeList/XmlNode object
  • Use an XmlNode object to iterate through the
    XmlNodeList and process the results

49
Code Sample - DOM XPath Queries
  • Sample XML document
  • lt?xml version"1.0"?gt
  • ltBooksgt
  • ltBook ISBN"0355605172"/gt
  • ltTitlegtThe C Programming languagelt/Titlegt
  • lt/Bookgt
  • ltBook ISBN"0735605173"/gt
  • ltTitlegtXML Step by Steplt/Titlegt
  • lt/Bookgt
  • lt/Booksgt
  • Sample XPath query
  • Select all titles that begin with the word XML
  • XPath Query //Titlestarts-with(.,XML)

50
Code Sample - DOM XPath Queries (2)
  • Dim xmlDoc as New XmlDocument
  • Dim matchingNodes as XmlNodeList
  • Dim matchingNode as XmlNode
  • xmlDoc.Load c\books.xml
  • matchingNodes xmlDoc.SelectNodes(//Titlestarts
    -with(.,XML))
  • If matchingNodes.Count 0 Then
  • MsgBox("No matching nodes were identified
    for the specified XPath query)
  • Else
  • For Each matchingNode In matchingNodes
  • MsgBox(matchingNode.Name "
    " matchingNode.InnerText)
  • Next
  • End If

51
The System.XML.XPath Classes
  • XPathDocument
  • Similar to the XmlDocument DOM object
  • Highly optimized for XSLT processing and XPath
  • Differences from the DOM object
  • Does not maintain node identity
  • Does not validate the XML document
  • XPathNavigator
  • Used to execute an XPath query against an
    XPathDocument (Select method)
  • Used to compile frequently used XPath query
    expressions (Compile method)
  • Instantiated using the CreateNavigator() method
    of the XPathDocument class

52
The System.XML.XPath Classes (2)
  • XPathNodeIterator
  • Provides a read-only cursor style interface to
    navigate the results generated by executing an
    XPath query
  • Generated by executing the Select(ltXPath
    query/expressiongt) method of an XPathNavigator
    object
  • XPathExpression
  • Used to store a compiled instance of a frequently
    used XPath query expression
  • Generated by executing the Compile(ltXPath
    querygt) method of an XPathNavigator object
  • Can be supplied as the input parameter of the
    XPathNavigators Select method

53
The System.XML.XPath Classes (3)
Load XML
XPathDocument
.CreateNavigator()
.Compile(ltXPath querygt)
XPathNavigator
Select(ltXPath Querygt)
XPathExpression
Select(XPathExpression)
XPathNodeIterator
Do While .MoveNext() Process results Loop
54
Code Sample Using the System.Xml.XPath Classes
  • Dim XPathDoc as XPathDocument
  • Dim XPathNav as XPathNavigator
  • Dim XPathExpr as XPathExpression
  • Dim XPNodeIterator as XPathNodeIterator
  • XPathDoc New XPathDocument(c\books.xml)
  • XPathNav XPathDoc.CreateNavigator
  • XPathExpr XPathNav.Compile(//Titlestarts-with(
    .,XML))
  • XPNodeIterator XPathNav.Select(XPathExpr)
  • If XPNodeIterator.Count 0 Then
  • MsgBox("No matching nodes were identified
    for the specified XPath query)
  • Else
  • Do While XPNodeIterator.MoveNext
  • MsgBox(XPNodeIterator.Current.Val
    ue)
  • Loop
  • End If

55
What Did We Cover Today?
  • Introduced the XML related .NET Framework
    namespaces
  • Introduced the XML parsing models in the .NET
    Framework
  • Examined the implementation of pull model XML
    parsing in the .NET Framework
  • Examined DOM parsing in the .NET Framework
  • Examined the process of programmatically
    generating XML in the .NET Framework
  • Examined the process of programmatically
    validating XML documents
  • Examined the process of programmatically
    executing XSLT transformations
  • Examined the process of programmatically
    executing XPath queries

56
Thank you for joining us for Todays Microsoft
Support WebCast For information on all upcoming
Support WebCasts and access to the archived
content (streaming media, Power Point slides, and
transcripts), please visit http//support.micros
oft.com/WebCasts We sincerely appreciate your
feedback. Please send any comments or suggestions
regarding the Support WebCasts to
feedback_at_microsoft.com. Include Support WebCasts
in the subject line
Write a Comment
User Comments (0)
About PowerShow.com