MIS 426 - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

MIS 426

Description:

Another use for XSL is for data format conversion ... You can use the XslTransform class to transform an XML document then save it to the file system ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 16
Provided by: nic166
Category:
Tags: mis

less

Transcript and Presenter's Notes

Title: MIS 426


1
MIS 426
  • Chapter 13, Using XML with DataSets

2
Overview of XML Classes
  • The .Net framework contains a rich set of classes
    for working with XML
  • This will only provide an overview of a few of
    those classes

3
Objectives
  • In this chapter you should learn how to
  • Use XML with DataSets
  • Transform XML with XSL Stylesheets
  • Use the ASP.NET Xml Control
  • Use the XslTransform Class
  • Use Strongly Typed DataSets (yea!)

4
Overview of XML Classes
  • The main classes are
  • XmlTextReader This class is for fast forward
    only access to the raw data in an XML file (read
    XML from a file)
  • XmlTextWriter This class is a fast forward
    only method to write data to an XML file (write
    XML to a file)
  • XmlDocument This class represents XML using the
    Document Object Model (DOM) to either navigate or
    edit the nodes in the XML document in a dataset
  • XmlDataDocument This class allows you to
    represent both XML and relational data structures
    in the DOM. This can be used with a DataSet to
    provide both relational and non-relational
    (tree)views of the same data

5
Overview of XML Classes
  • XmlNodeReader This class provides fast forward
    only access to data represented by the
    XmlDocument or XmlDataDocument class
  • XslTransform This class enables you to
    transform an XML document into an HTML document
    with XSL stylesheets

6
Using XML with DataSets
  • A DataSet can represent XML data in two ways
  • You can load XML data directly into a
    DataSetdsMenu.ReadXml( MapPath( "ReadMenu.xml" )
    )
  • View an Example
  • You can build an XmlDataDocument class from an
    existing data set
  • You can bind XML data to any of the standard
    ASP.NET controls that have a DataSource Property

7
Using a Schema with ReadXML so you can strongly
type the data (not all treated as strings)
  • You can specify a schema with an XML file in two
    ways
  • You can include the schema in the same document
    as the XML filedsMenu.ReadXml(MapPath(
    "ReadSchemaData.xml" ))
  • View an Example
  • You can read the schema separately by using the
    ReadXmlSchema Method
  • View an Example

8
Writing an XML Document from a DS
  • You can retrieve a string representation of the
    XML data with the GetXML() Method.
  • This allows you to output an XML representation
    of a DataSet to the browserstrXmlData
    dstTitles.GetXml()Response.Write( "ltpregt"
    Server.HtmlEncode( strXmlData ) "lt/pregt" )
  • View an Example
  • You can also use this to write an XML
    representation to a file on the hard
    drivedstTitles.WriteXml( "c\myXml.xml" )
  • View an Example

9
Using XMLDataDocs with Datasets
  • Sometimes it makes more sense to represent XML
    data as nodes on a tree as opposed to relational
    (tabular) data
  • View an example of relation data that would be
    better displayed as nodes
  • XMLDataDocuments represents data as nodes on a
    tree. You create a new XMLDataDocument by using
    a Dataset in the XML DataDocuments constructor
  • objDataDocument New XmlDataDocument(
    dstRecipes) objRecipes objDataDocument.GetElemen
    tsByTagName( "Ingredient" )
  • The GetElementsByTagName method retrievesa list
    of nodes that have the tagname identified View
    the data displayed as nodes

10
Transforming XML with XSL Stylesheets
  • One common use for XSL stylesheets is to convert
    XML into nicely formatted HTML
  • Another use for XSL is for data format conversion
  • To accomplish this you can use the ASP.NET Xml
    Control or you can use the XSLTransform Class

11
Using the ASP.NET Xml Control
  • You can use this control to simply display raw
    XML or you can use it with a stylesheet to format
    the XML
  • The XML control supports the following
    properties
  • DocumentSource The path to the XML file
  • TransformSource The path to the XSL file
  • Document A preloaded XML document
  • Transform a preloaded XSL stylesheet
  • View an Example

12
Using the XslTransform Class
  • The XslTransform class enables you to work with
    the XSL transform without displaying the results
  • You can use the XslTransform class to transform
    an XML document then save it to the file system
  • View an Example

13
Using Strongly Typed DataSets
  • You can access data in a DataSet by working with
    collections (such as tables, ros, columns) using
    string literals
  • or you can access the data by using strongly
    typed methods and properties specified by an XML
    schema
  • With strongly typed DataSets you can refer to
    tables and columns as named properties of the
    dataset (EX label1.text dsEmployees.Firstname
    (0))

14
Using Strongly Typed DataSets
  • When you create a typed DataSet you create a new
    class that represents the DataSet
  • To create a strongly typed DataSet you must
  • Create an XML schema that represents the
    structure of the DataSet
  • Use the XML schema with the Xsd.exe command line
    tool to generate the source code for a class that
    represents the DataSet
  • Compile the source code for the class and copy
    the compiled class into your applications /bin
    directory

15
To Create a Strongly Typed DataSet
  • View the XML Schema for the Titles table in the
    Pubs database
  • View the code and get the file
  • Copy the resulting file and save it
  • Open the command line and execute the following
    code Xsd.exe /d /lvb FileName.xsd
  • Now compile the this file vbc /tlibrary
    /rSystem.dll,System.Data.dll,System.Xml.dll
    TitlesDS.vb
  • Now copy this file to the /bin directory
  • View an example of how to utilize this strongly
    typed DataSet
Write a Comment
User Comments (0)
About PowerShow.com