Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

2310 b 12


Published on

Published in: Technology, News & Politics
  • Be the first to comment

2310 b 12

  1. 1. Module 12: Reading and Writing XML Data
  2. 2. Overview <ul><li>Overview of XML Architecture in ASP.NET </li></ul><ul><li>XML and the DataSet Object </li></ul><ul><li>Working with XML Data </li></ul><ul><li>Using the XML Web Server Control </li></ul>
  3. 3. Lesson: Overview of XML Architecture in ASP.NET <ul><li>What is XML? </li></ul><ul><li>XML Core Technologies </li></ul>
  4. 4. What is XML? <?xml version=&quot;1.0&quot;?> <authors> <author ID=&quot;1&quot;> <name>Jay</name> </author> <!-- There are more authors. --> </authors> <ul><li>Provides a uniform method for describing and exchanging structured data </li></ul><ul><li>You can define your own elements and attributes </li></ul><ul><li>Elements can be nested </li></ul><ul><li>Valid XML vs. Well-formed XML </li></ul>Processing Instruction Elements Attributes Comments
  5. 5. XML Core Technologies <ul><li>XML Schema definition </li></ul><ul><ul><li>Defines the required structure of a valid XML document </li></ul></ul><ul><li>Extensible Stylesheet Language Transformation </li></ul><ul><ul><li>Transforms the content of a source XML document into another document that is different in format or structure </li></ul></ul><ul><li>XML Path Language </li></ul><ul><ul><li>Addresses parts of an XML document </li></ul></ul><ul><li>Document Object Model </li></ul><ul><ul><li>Object model for programmatically working with XML documents in memory </li></ul></ul><ul><li>XML Query </li></ul><ul><ul><li>Easily implementable language in which queries are concise and easily understood </li></ul></ul>
  6. 6. Lesson: XML and the DataSet Object <ul><li>Why use XML with DataSets? </li></ul><ul><li>Overview of XML and DataSets </li></ul><ul><li>The XML-Based Methods of the DataSet Object </li></ul><ul><li>Demonstration: Reading and Writing XML to and from a DataSet </li></ul><ul><li>Practice: Using the ReadXml Method </li></ul><ul><li>Creating Nested XML Data </li></ul><ul><li>Demonstration: Creating Nested XML </li></ul>
  7. 7. Why Use XML with Datasets? <ul><li>XML is the universal format for exchanging data on the Internet </li></ul><ul><li>Datasets serialize data as XML </li></ul><ul><li>XML provides a convenient format for transferring the contents of a dataset to and from remote clients </li></ul><ul><li>XML objects synchronize and transform data </li></ul>Human Readable Mainframe Readable XML File Or Stream Browser Readable Web Server DataSet Firewall
  8. 8. Overview of XML and Datasets XML File DataSet Object XmlDataDocument Object XslTransform Object XML or HTML File Database DataAdapter ReadXML Doc.Save WriteXML XSLT File XML File XML File
  9. 9. <ul><li>Use ReadXml to load data from a file or stream </li></ul><ul><li>Use WriteXml to write XML data to a file or stream </li></ul><ul><li>Use GetXml to write data to a string variable </li></ul>The XML-Based Methods of the DataSet Object DataSet ds = new DataSet(); ds.ReadXml(Server.MapPath(&quot;filename.xml&quot;)); DataSet ds = new DataSet(); SqlDataAdapter da = new SqlDataAdapter(&quot;select * from Authors&quot;, conn); da.Fill(ds); ds.WriteXml(Server.MapPath(&quot;filename.xml&quot;)); string strXmlDS = ds.GetXml(); Visual Basic .NET Code Example
  10. 10. Demonstration: Reading and Writing XML to and from a DataSet <ul><li>Reading XML </li></ul><ul><li>Create a DataSet </li></ul><ul><li>Load DataSet from an XML file </li></ul><ul><li>Display in DataGrid </li></ul><ul><li>Writing XML </li></ul><ul><li>Create DataSet from database </li></ul><ul><li>Create an XML file from a DataSet </li></ul>
  11. 11. Practice: Using the ReadXml Method <ul><li>Students will: </li></ul><ul><ul><li>Create a DataSet </li></ul></ul><ul><ul><li>Load a DataSet from an XML file </li></ul></ul><ul><ul><li>Display in a DataGrid </li></ul></ul><ul><li>Time: 5 Minutes </li></ul>
  12. 12. <ul><li>By default, the output of DataTables is sequential </li></ul><ul><li>To make XML nested, make the DataRelation nested </li></ul><ul><li>Sequential Nested </li></ul>Creating Nested XML Data Dim dr As New DataRelation _ (&quot;name&quot;, parentCol, childCol) dr.Nested = True ds.Relations.Add(dr) <Title name=&quot;title1&quot; /> <Title name=&quot;title2&quot; /> <Title name=&quot;title3&quot; /> <Publisher name=&quot;pub1&quot; /> <Publisher name=&quot;pub2&quot; /> <Publisher name=&quot;pub1&quot; > <Title name=&quot;title1&quot; /> <Title name=&quot;title3&quot; /> </Publisher> <Publisher name=&quot;pub2&quot; > <Title name=&quot;title2&quot; /> </Publisher> DataRelation dr = new DataRelation(&quot;name&quot;, parentCol, childCol); dr.Nested = true; ds.Relations.Add(dr);
  13. 13. Demonstration: Creating Nested XML <ul><li>WriteXml out of a DataSet without nesting </li></ul><ul><li>View the resulting XML file </li></ul><ul><li>WriteXml out of a DataSet with nesting </li></ul><ul><li>View the resulting XML file </li></ul>
  14. 14. Lesson: Working with XML Data <ul><li>Overview of Synchronizing a DataSet with an XmlDataDocument </li></ul><ul><li>How to Synchronize a DataSet with an XmlDataDocument </li></ul><ul><li>Working with an XmlDataDocument </li></ul><ul><li>Transforming XML Data with XSLT </li></ul><ul><li>Demonstration: Transforming Data with XSLT </li></ul>
  15. 15. Overview of Synchronizing a DataSet with an XmlDataDocument Database DataAdapter DataSet Tables XmlDataDocument XML Transformations Other XML Document Types XML Document Navigation Synchronized System.Data System.Xml
  16. 16. <ul><li>Store XML Data into an XmlDataDocument </li></ul><ul><li>Store a DataSet in an XmlDataDocument </li></ul>How to Synchronize a DataSet with an XmlDataDocument Dim ds As New DataSet() 'fill in ds Dim objXmlDataDoc As New XmlDataDocument(ds) XmlDataDocument objXmlDataDoc = new XmlDataDocument(); objXmlDataDoc.Load(Server.MapPath (&quot;file.xml&quot;)); -or- objXmlDataDoc.DataSet.ReadXml(Server.MapPath (&quot;file.xml&quot;)); DataSet ds = new DataSet(); //fill in ds objXmlDataDoc = new XmlDataDocument(ds); Dim objXmlDataDoc As New XmlDataDocument() objXmlDataDoc.Load(Server.MapPath (&quot;file.xml&quot;)) -or- objXmlDataDoc.DataSet.ReadXml(Server.MapPath (&quot;file.xml&quot;))
  17. 17. Working with an XmlDataDocument <ul><li>Display data in a list-bound control </li></ul><ul><li>Extract Dataset rows as XML </li></ul><ul><li>Use XML DOM methods </li></ul><ul><ul><li>XmlDataDocument inherits from XmlDocument </li></ul></ul><ul><li>Apply an XSLT transformation </li></ul><ul><ul><li>XslTransform object </li></ul></ul>dg.DataSource = objXmlDataDoc.DataSet Dim elem As XmlElement elem = objXmlDataDoc.GetElementFromRow _ (ds.Tables(0).Rows(1)) XmlElement elem; elem = objXmlDataDoc.GetElementFromRow(ds.Tables[0].Rows[1]); dg.DataSource = objXmlDataDoc.DataSet;
  18. 18. Transforming XML Data with XSLT <ul><li>Create XmlDataDocument </li></ul><ul><li>Create XSLTransform object and call Transform method </li></ul>Dim ds As New DataSet() 'fill in DataSet ... Dim xmlDoc As New XmlDataDocument(ds) Dim xslTran As New XslTransform() xslTran.Load(Server.MapPath(&quot;PubTitles.xsl&quot;)) Dim writer As New XmlTextWriter _ (Server.MapPath(&quot;PubTitles_output.html&quot;), _ System.Text.Encoding.UTF8) xslTran.Transform(xmlDoc, Nothing, writer) writer.Close() C# Code Example
  19. 19. Demonstration: Transforming Data with XSLT <ul><li>Create a DataSet with two DataTables </li></ul><ul><li>Create XslTransform </li></ul><ul><li>Transform the DataSet into HTML document </li></ul>
  20. 20. Lesson: Using the XML Web Server Control <ul><li>What is the XML Web Server Control? </li></ul><ul><li>Loading and Saving XML Data </li></ul><ul><li>Demonstration: Using the XML Web Server Control </li></ul>
  21. 21. What is the XML Web Server Control? <ul><li>Write to an XML document </li></ul><ul><li>Writes the results of an XSLT Transformations into a Web page </li></ul><asp:Xml id=&quot; Xml1 &quot; Document=&quot; XmlDocument object to display &quot; DocumentContent=&quot; String of XML &quot; DocumentSource=&quot; Path to XML Document &quot; Transform=&quot; XslTransform object &quot; TransformSource=&quot; Path to XSL Document &quot; runat=&quot;server&quot;/>
  22. 22. Loading and Saving XML Data <ul><li>XML Web server control (in the Web Form) </li></ul><ul><li>Loading data dynamically (in the code-behind page) </li></ul><ul><li>Saving data (in the code-behind page) </li></ul>xmlCtl.Document.Save(Server.MapPath(&quot;text.xml&quot;)) xmlCtl.Document.Load(Server.MapPath(&quot;text.xml&quot;)) <asp:Xml id=&quot;xmlCtl&quot; runat=&quot;server&quot; /> xmlCtl.Document.Save(Server.MapPath(&quot;text.xml&quot;)) ; xmlCtl.Document.Load(Server.MapPath(&quot;text.xml&quot;));
  23. 23. Demonstration: Using the XML Web Server Control <ul><li>Add the XML Web server control to a Web Form </li></ul><ul><li>Set the DocumentSource property to read an XML file </li></ul><ul><li>View the result </li></ul><ul><li>Set the TransformSource property to read an XSLT file </li></ul><ul><li>View the result </li></ul>
  24. 24. Review <ul><li>Overview of XML Architecture in ASP.NET </li></ul><ul><li>XML and the DataSet Object </li></ul><ul><li>Working with XML Data </li></ul><ul><li>Using the XML Web Server Control </li></ul>
  25. 25. Lab 12: Reading XML Data Medical Medical.aspx Benefits Home Page Default.aspx Life Insurance Life.aspx Retirement Retirement.aspx Dental Dental.aspx Dentists Doctors Doctors.aspx Doctors Logon Page Login.aspx Registration Register.aspx Coho Winery Prospectus Prospectus.aspx XML Web Service dentalService1.asmx Page Header Header.ascx ASPState tempdb Lab Web Application User Control namedate.ascx Menu Component Class1.vb or Class1.cs XML Files Web. config