CIS 451: USING ASP.NET TO CREATE XML
Dr. Ralph D. WestfallFebruary, 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
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
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
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
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>")
Code - 5
write file lines objSW.WriteLine("<name>" _
& strName & "</name>") objSW.WriteLine("<street>" _
& strStreet & "</street>") write closing tag
objSW.WriteLine("</info>")
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
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
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
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
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