270 likes | 497 Views
Module 12: Reading and Writing XML Data. Overview. Overview of XML Architecture in ASP.NET XML and the DataSet Object Working with XML Data Using the XML Web Server Control. Lesson: Overview of XML Architecture in ASP.NET. What is XML? XML Core Technologies. What is XML?.
E N D
Overview • Overview of XML Architecture in ASP.NET • XML and the DataSet Object • Working with XML Data • 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 • Valid XML vs. Well-formed XML Attributes Processing Instruction <?xml version="1.0"?> <authors> <author ID="1"> <name>Jay</name> </author> <!-- There are more authors. --> </authors> Elements Comments
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 • Demonstration: Reading and Writing XML to and from a DataSet • Practice: Using the ReadXml Method • 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 Or Stream Browser Readable DataSet Mainframe Readable
Overview of XML and Datasets WriteXML XML File XML File DataAdapter Doc.Save DataSet Object XmlDataDocument Object Database XslTransform Object ReadXML XML File XML or HTML File XSLT File
The XML-Based Methods of the DataSet Object • Use ReadXml to load data from a file or stream • Use WriteXml to write XML data to a file or stream • Use GetXml to write data to a string variable DataSet ds = new DataSet(); ds.ReadXml(Server.MapPath("filename.xml")); DataSet ds = new DataSet(); SqlDataAdapter da = new SqlDataAdapter("select * from Authors", conn); da.Fill(ds); ds.WriteXml(Server.MapPath("filename.xml")); string strXmlDS = ds.GetXml(); Visual Basic .NET Code Example
Demonstration: Reading and Writing XML to and from a DataSet Reading XML • Create a DataSet • Load DataSet from an XML file • Display in DataGrid Writing XML • Create DataSet from database • Create an XML file from a DataSet
Practice: Using the ReadXml Method • Students will: • Create a DataSet • Load a DataSet from an XML file • Display in a DataGrid • Time: 5 Minutes
Creating Nested XML Data • By default, the output of DataTables is sequential • To make XML nested, make the DataRelation nested Sequential Nested Dim dr As New DataRelation _ ("name", parentCol, childCol) dr.Nested = True ds.Relations.Add(dr) DataRelation dr = new DataRelation("name", parentCol, childCol); dr.Nested = true; ds.Relations.Add(dr); <Title name="title1" /> <Title name="title2" /> <Title name="title3" /> <Publisher name="pub1" /> <Publisher name="pub2" /> <Publisher name="pub1" > <Title name="title1" /> <Title name="title3" /> </Publisher> <Publisher name="pub2" > <Title name="title2" /> </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: Working with XML Data • Overview of Synchronizing a DataSet with an XmlDataDocument • How to Synchronize a DataSet with an XmlDataDocument • Working with 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 Synchronized Tables
How to Synchronize a DataSet with an XmlDataDocument • Store XML Data into an XmlDataDocument • Store a DataSet in an XmlDataDocument Dim objXmlDataDoc As New XmlDataDocument() objXmlDataDoc.Load(Server.MapPath ("file.xml")) -or- objXmlDataDoc.DataSet.ReadXml(Server.MapPath ("file.xml")) XmlDataDocument objXmlDataDoc = new XmlDataDocument(); objXmlDataDoc.Load(Server.MapPath ("file.xml")); -or- objXmlDataDoc.DataSet.ReadXml(Server.MapPath ("file.xml")); Dim ds As New DataSet() 'fill in ds Dim objXmlDataDoc As New XmlDataDocument(ds) DataSet ds = new DataSet(); //fill in ds objXmlDataDoc = new XmlDataDocument(ds);
Working with 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 dg.DataSource = objXmlDataDoc.DataSet 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]);
Transforming XML Data with XSLT • Create XmlDataDocument • Create XSLTransform object and call Transform method Dim ds As New DataSet() 'fill in DataSet ... Dim xmlDoc As New XmlDataDocument(ds) Dim xslTran As New XslTransform() xslTran.Load(Server.MapPath("PubTitles.xsl")) Dim writer As New XmlTextWriter _ (Server.MapPath("PubTitles_output.html"), _ System.Text.Encoding.UTF8) xslTran.Transform(xmlDoc, Nothing, writer) writer.Close() C# Code Example
Demonstration: Transforming Data with XSLT • Create a DataSet with two DataTables • Create XslTransform • Transform the DataSet into HTML document
Lesson: Using the XML Web Server Control • What is the XML Web Server Control? • Loading and Saving XML Data • Demonstration: Using the XML Web Server Control
What is the XML Web Server Control? • Write to an XML document • Writes the results of an XSLT Transformations 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) • Loading data dynamically (in the code-behind page) • Saving data (in the code-behind page) <asp:Xml id="xmlCtl" runat="server" /> xmlCtl.Document.Load(Server.MapPath("text.xml")) xmlCtl.Document.Load(Server.MapPath("text.xml")); xmlCtl.Document.Save(Server.MapPath("text.xml")) xmlCtl.Document.Save(Server.MapPath("text.xml"));
Demonstration: Using the XML Web Server Control • Add the XML Web server control to a Web Form • Set the DocumentSource property to read an XML file • View the result • Set the TransformSource property to read an XSLT file • View the result
Review • Overview of XML Architecture in ASP.NET • XML and the DataSet Object • Working with XML Data • Using the XML Web Server Control
Lab 12: Reading XML Data Logon Page Login.aspx BenefitsHome PageDefault.aspx CohoWinery Page HeaderHeader.ascx ASPState Menu ComponentClass1.vb or Class1.cs Registration Register.aspx Web.config tempdb Life InsuranceLife.aspx RetirementRetirement.aspx MedicalMedical.aspx DentalDental.aspx XML Web ServicedentalService1.asmx ProspectusProspectus.aspx DoctorsDoctors.aspx User Controlnamedate.ascx Lab Web Application XML Files Doctors Dentists