12
CIS 451: USING ASP.NET TO CREATE XML Dr. Ralph D. Westfall February, 2009

CIS 451: USING ASP.NET TO CREATE XML Dr. Ralph D. Westfall February, 2009

Embed Size (px)

Citation preview

Page 1: CIS 451: USING ASP.NET TO CREATE XML Dr. Ralph D. Westfall February, 2009

CIS 451: USING ASP.NET TO CREATE XML

Dr. Ralph D. WestfallFebruary, 2009

Page 2: CIS 451: USING ASP.NET TO CREATE XML Dr. Ralph D. Westfall February, 2009

Creating XML with a Program create ASP.NET page with controls

with Visual Web Developer Express <asp:textbox [attributes]>

or use an HTML form with input areas use text input fields (1 line) for short

items use textareas for longer inputs

specify # of rows and columns set wrap = on

Page 3: CIS 451: USING ASP.NET TO CREATE XML Dr. Ralph D. Westfall February, 2009

UI and Code File add 6 TextBoxes to Default.aspx

set TextMode to Multiline on TextBox5 put a Button after the 5th TextBox

click Button to create sub in Default.aspx.vb put Imports System.IO as first line declare objSW as a StreamWriter object

Page 4: CIS 451: USING ASP.NET TO CREATE XML Dr. Ralph D. Westfall February, 2009

Code File - 2 declare/instantiate the following

Strings strFile, strName, strStreet, strCity,

strComments assign TextBox[].Text values to the above Dim strQt = Chr(34) ' a single quote

add a folder named xml to project right click project in Solution Explorer,

select New Folder, rename it xml

Page 5: CIS 451: USING ASP.NET TO CREATE XML Dr. Ralph D. Westfall February, 2009

Code - 3 Dim strFullPath as String =

Request.PhysicalApplicationPath & "xml\" & strFile & ".xml" 'period

add a Try/Catch block Try Catch err As Exception TextBox6.Text = "Error: " &

err.Message End Try

Page 6: CIS 451: USING ASP.NET TO CREATE XML Dr. Ralph D. Westfall February, 2009

Code - 4 put file writing code in the Try block

objSW = File.CreateText(strFullPath) write XML header

objSW.WriteLine("<?xml version=" _ & strQt & "1.0" & strQt & " ?>")

write XML root tag objSW.WriteLine("<info>")

Page 7: CIS 451: USING ASP.NET TO CREATE XML Dr. Ralph D. Westfall February, 2009

Code - 5

write file lines objSW.WriteLine("<name>" _

& strName & "</name>") objSW.WriteLine("<street>" _

& strStreet & "</street>") write closing tag

objSW.WriteLine("</info>")

Page 8: CIS 451: USING ASP.NET TO CREATE XML Dr. Ralph D. Westfall February, 2009

Code and UI End Try block and close .xml file

after finished writing lines to it objSW.Close

go back to Default.aspx drag and drop a hyperlink onto

form delete its Text property

Page 9: CIS 451: USING ASP.NET TO CREATE XML Dr. Ralph D. Westfall February, 2009

Configure Link to XML File put the following code after End

Try statement in Default.aspx.vb strFile &= ".xml" HyperLink1.Text = strFile HyperLink1.NavigateUrl = "xml\" &

strFile

Page 10: CIS 451: USING ASP.NET TO CREATE XML Dr. Ralph D. Westfall February, 2009

Run Default.aspx Click the link to view the xml file

if the link doesn't load the .xml page, go to the xml subdirectory in Solution Explorer and uncheck Read Only

could also right click the xml folder in Solution Explorer, select Refresh Folder and then double click the xml file name

Page 11: CIS 451: USING ASP.NET TO CREATE XML Dr. Ralph D. Westfall February, 2009

Exercise see makereport.aspx for code

generated by these instructions add more/different input controls

to Default.aspx modify code to write their contents

into XML also add elements with sub-elements test code, correct as necessary

Page 12: CIS 451: USING ASP.NET TO CREATE XML Dr. Ralph D. Westfall February, 2009

Exercise - 2 create DTD and CSS files to work

with the XML files that your code is writing put them into the directory that the

XML files are being written to modify the code in Default.aspx to

write the links to these files into the XML files