WC121801 by Gd66VQcd

VIEWS: 6 PAGES: 56

									Programming XML in the
Microsoft .NET Framework
– Part I

Karthik Ravindran
Karthi Karthikeyan
PSS XML Web Database
Microsoft Corporation
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

                                                2
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



                                              3
A Bird‟s Eye View of the .NET Framework
 ASP.NET Applications

   ASP.NET Web    Windows Forms        ASP.NET Web     Windows Services
   Applications    Applications          Services




                  .NET Framework Base classes
  ADO.NET               XML             SECURITY             NETWORK

  SECURITY        MESSAGING             WINFORMS           WEB      ETC.,


                    Common Language Runtime
   Memory          Object Life cycle     Common Type         JIT
   Management      Management            System              Compilers


                         Operating System

                              MACHINE                                       4
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)
                                                       5
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                                               6
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


                                                7
Programming the XML DOM Using
  the System.Xml DOM Classes




                                8
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                                                9
The Key System.Xml DOM Classes
(Inheritance Hierarchy)
 Object

          XmlNode
                 XmlLinkedNode
                          XmlCharacterData           XmlCDATASection
                          XmlDeclaration             XmlComment
                          XmlDocumentType            XmlSignificantWhiteSpace
                          XmlElement                 XmlWhiteSpace
                          XmlEntityRef
                          XmlProcessingInstruction
                 XmlAttribute
                 XmlDocument
                 XmlDocumentFragment
                 XmlDocumentType
                 XmlEntity
                 XmlNotation
          XmlNamedNodeMap
                 XmlAttributeCollection
          XmlNodeList
                                                                            10
The Key System.Xml DOM Classes
(Aggregation Hierarchy)
XmlDocument
     XmlNodeList
            XmlNode
                   XmlLinkedNode
                           XmlCharacterData           XmlCDATASection
                           XmlDeclaration             XmlComment
                           XmlDocumentType            XmlSignificantWhiteSpace
                           XmlElement                 XmlWhiteSpace
                           XmlEntityRef
                           XmlProcessingInstruction
                   XmlNamedNodeMap
                                        XmlAttributeCollection
                    XmlDocumentType                    XmlAttribute
                    XmlEntity
                    XmlNotation
                                                                            11
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
                                                  12
Code Sample: DOM Parsing in
System.Xml
(Sample XML Document)
<?xml version="1.0"?>
<Books>
 <Book ISBN="0355605172"/>
   <Title>Beginning XML</Title>
   <Price>40.00</Price>
 </Book>
<Book ISBN="0415205173"/>
   <Title>XML Step by Step</Title>
   <Price>50.00</Price>
 </Book>
</Books>                             13
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 >= 50 Then
         Price = Price - ((5 / 100) * Price)
         PriceNode.InnerText = CType(Price, String)
      End If
Next

xmldoc.Save("c:\books.xml")                           14
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")                          15
Implementing Non-Cached Pull
Model XML Parsing in the .NET
        Framework



                                16
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      17
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
                                                          18
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.
                                                             19
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)
reader.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
                                                                               20
Dynamically Generating XML by
    Programming the .NET
   Framework XML Classes




                                21
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




                                                22
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

                                                    23
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)




                                                 24
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                                    25
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

                                                    26
Code Sample: Using the
XmlTextWriter
(Sample XML Document)
<?xml version="1.0"?>
<!--Catalog fragment-->
<!DOCTYPE Books SYSTEM "books.dtd">
<Books>
 <Book ISBN="0355605172"/>
   <Title>XML Step by Step</Title>
 </Book>
</Books>
                                      27
Code Sample: Using the XmlTextWriter to
Generate XML
     Dim wrt As XmlTextWriter = New XmlTextWriter("c:\books.xml", Nothing)
     wrt.Formatting = System.Xml.Formatting.Indented

     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()

     wrt.Close()                                                             28
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(GenerateBookNode(xmldoc, "XML Step by Step", "0355605172"))

xmldoc.Save("c:\books2.xml")
                                                                                                 29
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.CreateAttribute("ISBN"))
    BookNode.Attributes.GetNamedItem("ISBN").InnerText = ISBN

    GenerateBookNode = BookNode

End Function                                                        30
Validating XML Documents in the
         .NET Framework




                                  31
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.
                                                                      32
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.
                                                 33
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                                34
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/2001/XMLSchema XSD
    recommendation.
   Does not support caching DTDs.
   Any method and property that takes or
    returns an XmlSchema applies only to XSD
    schemas.                                      35
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 > 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
    End If

End Sub

Public Shared Sub ValidationCallBack(ByVal sender As Object, ByVal e As ValidationEventArgs)
    Console.WriteLine("XSD Error: {0}", e.Message)
                                                                                               36
End Sub
Executing XSLT Transformations
    in the .NET Framework




                                 37
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.   38
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                                       39
XSLT Architecture in .NET




                            40
XslTransform Class
   The XSLT stylesheet must include the
    namespace declaration "xmlns:xsl=
    http://www.w3.org/1999/XSL/Transform"
   Key methods:
         Load
             Loads the XSLT stylesheet including any xsl:include
              and xsl:import 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.                                                  41
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.xml", "output.xml")

' Transforms the XML data in the XPathDocument and outputs the result to an XmlReader
Dim URL As String = "http://samples.gotdotnet.com/quickstart/howto/samples/Xml/TransformXml/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)

                                                                                             42
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
                                                                      43
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("order.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)
                                                                                      44
 Executing XPath Queries Using
the .NET Framework XML Classes




                                 45
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


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

                                                  47
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            48
Code Sample - DOM XPath
Queries
 Sample XML document:
    <?xml version="1.0"?>
    <Books>
     <Book ISBN="0355605172"/>
       <Title>The C Programming language</Title>
     </Book>
     <Book ISBN="0735605173"/>
       <Title>XML Step by Step</Title>
     </Book>
    </Books>
 Sample     XPath query:
      Select all titles that begin with the word „XML‟
         XPath Query : //Title[starts-with(.,‟XML‟)]     49
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(“//Title[starts-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                                                                  50
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                        51
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(<XPath
        query/expression>) method of an XPathNavigator
        object
   XPathExpression
       Used to store a compiled instance of a frequently
        used XPath query expression
       Generated by executing the Compile(“<XPath
        query>”) method of an XPathNavigator object
       Can be supplied as the input parameter of the
        XPathNavigator‟s Select method
                                                            52
 The System.XML.XPath Classes (3)
 Load XML

              XPathDocument
                                         .CreateNavigator()


.Compile(“<XPath query>”)
                            XPathNavigator

                                                   Select(“<XPath Query>”)
  XPathExpression      Select(XPathExpression)




                                        XPathNodeIterator
                                                              Do While .MoveNext()
                                                                „Process results
                                                              Loop                53
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(“//Title[starts-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.Value)
   Loop
End If                                                                         54
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
                                                          55
Thank you for joining us for Today‟s 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.microsoft.com/WebCasts

We sincerely appreciate your feedback. Please send any
   comments or suggestions regarding the Support
   WebCasts to feedback@microsoft.com. Include
   Support WebCasts in the subject line
                                                          56

								
To top