Saving Web Form User Input Data to an XML File...
Instead of saving form data to a database table, you may need to save it to an XML file on disk. This article shows you how to do it.
We recently did an article on how to save the data from a DataSet to an XML file on disk. This is a companion article on how to save form data to an XML file on disk. There are any number of reasons to do this so you might as well know how to do it. There is some repetitive typing involved to get the code set up, but it is fairly straight ahead once you see the logic in it.
We will be using various methods of the XmlTextWriter class to get the job done. There is some pretty good documentation of XmlTextWriter's properties and methods at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemxmlxmltextwritermemberstopic.asp.
We will use a short name and address type of form to collect some data from the user. A submit button will be used to call a routine on our code-behind page to write the form data to an XML file on disk in the root directory of the current web. At the end of the code-behind file we will do a Response.Redirect to the XML file to display its contents on screen.
First the .aspx page where the form and button reside.
|
<%@ Page Language="vb" Src="WebFormToXml.aspx.vb" Inherits="WebFormToXml"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>WebFormToXml</title> <meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0"> <meta name="CODE_LANGUAGE" content="Visual Basic 7.0"> <meta name=vs_defaultClientScript content="JavaScript"> <meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5"> </head> <body MS_POSITIONING="GridLayout"> <form id="Form1" method="post" runat="server"> <table border="1" cellspacing="0" cellpadding="4"> <tr> <td bgcolor="EEEEEE" align="right">Last Name:</td> <td align="left"><asp:TextBox id="txtLastName" runat="server" /></td> <tr> <tr> <td bgcolor="EEEEEE" align="right">First Name:</td> <td align="left"><asp:TextBox id="txtFirstName" runat="server" /></td> </tr> <tr> <td bgcolor="EEEEEE" align="right">Middle Name:</td> <td align="left"><asp:TextBox id="txtMiddleName" runat="server" /></td> </tr> <tr> <td bgcolor="EEEEEE" align="right">Address:</td> <td align="left"><asp:TextBox id="txtAddress" runat="server" /></td> </tr> <tr> <td bgcolor="EEEEEE" align="right">City:</td> <td align="left"><asp:TextBox id="txtCity" runat="server" /></td> </tr> <tr> <td bgcolor="EEEEEE" align="right">State:</td> <td align="left"><asp:TextBox id="txtState" runat="server" /></td> </tr> <tr> <td bgcolor="EEEEEE" align="right">Zip Code:</td> <td align="left"><asp:TextBox id="txtZipCode" runat="server" /></td> </tr> <tr> <td align="center" colspan="2"> <asp:Button id="btnWriteXml" OnClick="Write_XML" Text="Write XML File" runat="server" /> </td> </tr> </table> </form> </body> </html> |
And now for the code-behind page. The bulk of this page is the code for our button click (Write_XML) event. This is where we use the XmlTextWriter to create the XML file from the web form data. In the first line of the sub-routine we declare the XmlTextWriter and pass in the path and file name of the xml file we will be writing to. Rather than explain all the code here, I have commented each line or section of code so you should be able to follow what the code is doing just by reading the code and then comparing it to the output that is produced at the end. The last line of code is: Response.Redirect(Server.MapPath("PersonData.xml")). This simply instructs the browser to load the xml file we have created on disk and display it in the browser window.
|
Imports System.Xml Imports System.Web.UI.WebControls Public Class WebFormToXml : Inherits System.Web.UI.Page Protected txtLastName As TextBox Protected txtFirstName As TextBox Protected txtMiddleName As TextBox Protected txtAddress As TextBox Protected txtCity As TextBox Protected txtState As TextBox Protected txtZipCode As TextBox Public Sub Write_XML(ByVal sender As System.Object, ByVal e As System.EventArgs) Dim textWriter As New XmlTextWriter (Server.MapPath("PersonData.xml"), Nothing) textWriter.Formatting = System.Xml.Formatting.Indented 'Start New Document textWriter.WriteStartDocument() 'Write a Comment textWriter.WriteComment("This is a comment") 'Insert Start Element textWriter.WriteStartElement("PersonalData") 'Write Attribute for Start Element textWriter.WriteAttributeString("DataType", "NameAndAddress") 'Write LastName Element and Data textWriter.WriteStartElement("LastName", "") textWriter.WriteString(txtLastName.Text) textWriter.WriteEndElement() 'Write FirstName Element and Data textWriter.WriteStartElement("FirstName", "") textWriter.WriteString(txtFirstName.Text) textWriter.WriteEndElement() 'Write MiddleName Element and Data textWriter.WriteStartElement("MiddleName", "") textWriter.WriteString(txtMiddleName.Text) textWriter.WriteEndElement() 'Write Address Element and Data textWriter.WriteStartElement("Address", "") textWriter.WriteString(txtAddress.Text) textWriter.WriteEndElement() 'Write City Element and Data textWriter.WriteStartElement("City", "") textWriter.WriteString(txtCity.Text) textWriter.WriteEndElement() 'Write State Element and Data textWriter.WriteStartElement("State", "") textWriter.WriteString(txtState.Text) textWriter.WriteEndElement() 'Write ZipCode Elment and Data textWriter.WriteStartElement("ZipCode", "") textWriter.WriteString(txtZipCode.Text) textWriter.WriteEndElement() 'End Everything textWriter.WriteEndDocument() 'Clean up textWriter.Flush() textWriter.Close() 'Display the XML Document Response.Redirect(Server.MapPath("PersonData.xml")) End Sub End Class |
I hope you have seen how simple it is to create xml documents on the fly from user input. The coding is perhaps a little "wordy", but it is easy to follow and is certainly easier than formatting and writing to a text file directly yourself.
You may download the code here.