Reading and Writing XML Data

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 25

Reading and Writing XML

Data
Module 10: Reading and Writing XML Data
• Overview of XML Architecture in ASP.NET

• XML and the DataSet Object

• Managing XML Data

• Accessing XML Data by Using the XML Web Server Control


Lesson: Overview of XML Architecture in ASP.NET
• What Is XML?

• XML Core Technologies


What Is XML?

• Provides a uniform method for describing and


exchanging structured data
• You can define your own elements and attributes

• Elements can be nested


Attributes

Processing
Instruction <?xml
<?xml version="1.0"?>
version="1.0"?>
<authors>
<authors>
<author
<author ID="1">
ID="1">
Elements
<name>Jay</name>
<name>Jay</name>
</author>
</author>
<!--
<!-- There
There are
are more
more authors.
authors. -->
--> Comments
</authors>
</authors>

• Valid XML vs. Well-formed XML


XML Core Technologies
• XML Schema definition
 Defines the required structure of a valid XML document

• Extensible Stylesheet Language Transformation


 Transforms the content of a source XML document into
another document that is different in format or structure

• XML Path Language


 Addresses parts of an XML document

• Document Object Model


 Object model for programmatically working with XML
documents in memory

• XML Query
 Easily implementable language in which queries are concise
and easily understood
Lesson: XML and the DataSet Object
• Why use XML with DataSets?

• Overview of XML and DataSets

• The XML-Based Methods of the DataSet Object

• Creating Nested XML Data

• Demonstration: Creating Nested XML


Why use XML with DataSets?
• XML is the universal format for exchanging data on the
Internet
• Datasets serialize data as XML

• XML provides a convenient format for transferring the


contents of a dataset to and from remote clients
• XML objects synchronize and transform data

Firewall
Human Readable
Web Server
XML File Browser Readable
DataSet Or
Stream Mainframe Readable
Overview of XML and DataSets

WriteXML

DataAdapter
XML File XML File
Doc.Save

DataSet
DataSet XmlDataDocument
XmlDataDocument
Database Object
Object Object
Object

XslTransform
XslTransform
Object
Object
ReadXML

XML File
XML or
XSLT File
HTML File
The XML-Based Methods of the DataSet Object

• Use ReadXml to load data from a file or stream

DataSet ds = new DataSet();


ds.ReadXml(Server.MapPath("filename.xml"));

• Use WriteXml to write XML data to a file or stream


DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter("select * from
Authors", conn);
da.Fill(ds);
ds.WriteXml(Server.MapPath("filename.xml"));

• Use GetXml to write data to a string variable

string strXmlDS = ds.GetXml();

Visual Basic .NET Code Example


Creating Nested XML Data

• By default, the output of DataTables is sequential

• To make XML nested, make the DataRelation nested

• Sequential Nested

<Title
<Title name="title1"
name="title1" />
/> <Publisher
<Publisher name="pub1"
name="pub1" >>
<Title
<Title name="title2"
name="title2" />
/> <Title
<Title name="title1"
name="title1" />
/>
<Title
<Title name="title3"
name="title3" />
/> <Title
<Title name="title3"
name="title3" />
/>
<Publisher
<Publisher name="pub1"
name="pub1" />
/> </Publisher>
</Publisher>
<Publisher
<Publisher name="pub2"
name="pub2" />
/> <Publisher
<Publisher name="pub2"
name="pub2" >>
<Title
<Title name="title2"
name="title2" />
/>
</Publisher>
</Publisher>
Demonstration: Creating Nested XML
• WriteXml out of a DataSet without nesting

• View the resulting XML file

• WriteXml out of a DataSet with nesting

• View the resulting XML file


Lesson: Managing XML Data
• Overview of Synchronizing a DataSet with an
XmlDataDocument
• Synchronizing a DataSet with an XmlDataDocument

• Manipulating an XmlDataDocument

• Transforming XML Data with XSLT

• Demonstration: Transforming Data with XSLT


Overview of Synchronizing a DataSet with an
XmlDataDocument

System.Data System.Xml
XML Transformations

Database Other XML Document Types

XML Document Navigation


DataAdapter

DataSet XmlDataDocument

Tables Synchronized
Synchronizing a DataSet with an
XmlDataDocument

• Store XML Data into an XmlDataDocument

• Store a DataSet in an XmlDataDocument


Manipulating an XmlDataDocument

• Display data in a list-bound control

• Extract Dataset rows as XML

• Use XML DOM methods


 XmlDataDocument inherits from XmlDocument

• Apply an XSLT transformation


 XslTransform object
Transforming XML Data with XSLT

• Create XmlDataDocument

• Create XSLTransform object and call Transform


method
Demonstration: Transforming Data with XSLT
• Create a DataSet with two DataTables

• Create XslTransform

• Transform the DataSet into HTML document


Lesson: Accessing XML Data by Using the XML
Web Server Control
• What Is the XML Web Server Control?

• Loading and Saving XML Data


What Is the XML Web Server Control?
• Write to an XML document

• Writes the results of an XSLT Transformation into a Web


page

<asp:Xml id="Xml1"
Document="XmlDocument object to display"
DocumentContent="String of XML"
DocumentSource="Path to XML Document"
Transform="XslTransform object"
TransformSource="Path to XSL Document"
runat="server"/>
Loading and Saving XML Data

• XML Web server control (in the Web Form)


<asp:Xml id="xmlCtl" runat="server" />

• Loading data dynamically (in the code-behind page)


xmlCtl.Document.Load(Server.MapPath("text.xml"))

xmlCtl.Document.Load(Server.MapPath("text.xml"));

• Saving data (in the code-behind page)


xmlCtl.Document.Save(Server.MapPath("text.xml"))

xmlCtl.Document.Save(Server.MapPath("text.xml"));
Reading XML Data
• Exercise 1: Reading a List of Mutual Funds from an XML
File
• Exercise 2: Reading, Transforming, and Displaying XML

• Exercise 3: (If Time Permits): Nested Data

Logon information
Virtual machine 2310C_10
User name Student
Password Pa$$w0rd

Estimated time: xx minutes


Lab Scenario

Logon Page
Login.aspx
Benefits
Coho Home Page Page Header ASPState
Winery Default.aspx Header.ascx
Menu
Registration Component
Register.aspx Class1.vb or Class1.cs Web.
tempdb
config

Life Insurance Retirement Medical Dental


Life.aspx Retirement.aspx Medical.aspx Dental.aspx

Prospectus Doctors User Control XML Web


Lab Web Prospectus.aspx Doctors.aspx namedate.ascx Service
Application dentalService1.asmx

XML
Doctors Dentists
Files
Lab Review
Module Review and Takeaways
Review Questions
• What is the advantage of using XML to manage data?

• What is the difference between a well-formed and XML


document and a valid XML document?
• What is the importance of XSD?

• What is the role of the XmlDataDocument class in the


XML architecture?
Review for Alpha
• Is there any topic or specific content item in the module
that seemed unclear or unnecessary?
• Is there any content item/related subject area that was
not covered and could be included?
• Did you observe any issues with the technical accuracy of
the content?
• Is the content in the module presented in a manner that
encourages learning? Did the flow of topics seem right?
• Does the lab outline indicate the expected scope of tasks
to be covered? Would you like to suggest any tasks that
could be removed or added?

You might also like