Library code snippets
How to write to an XML file
Writing to XML files is quite easy in ASP.NET as there are so many objects prepared for you -- you just need the syntax on how to use them. This code shows you how to write a simple member XML file.
<%@Page Language="VB" Debug="True" %>
<%@Import Namespace="System.XML" %>
<%@ Import Namespace="System.IO" %>
<script language="vb" runat="server">
Sub Page_Load()
Dim strCurrentPath As String = Request.PhysicalPath
Dim strXMLPath As String = Left(strCurrentPath, InStrRev(strCurrentPath,
"\")) & "members3.xml"
Dim objXMLWriter As XmlTextWriter
Try
objXMLWriter = New XmlTextWriter(strXMLPath, Nothing)
outDocURL.innerHTML = "Writing to file: <b>"
& strXMLPath & "</b>"
Catch objError As Exception
outError.innerHTML = "<b>* Error while accessing
document</b>.<br />" & objError.Message & "<br />" & objError.Source
Exit Sub
End Try
objXMLWriter.Formatting = Formatting.Indented
objXMLWriter.Indentation = 3
objXMLWriter.WriteStartDocument()
objXMLWriter.WriteComment("Created on " & Now())
objXMLWriter.WriteStartElement("members")
objXMLWriter.WriteStartElement("member")
objXMLWriter.WriteAttributeString("position", "regular")
Dim intYears As Integer = 4
objXMLWriter.WriteAttributeString("years", intYears.ToString("G"))
objXMLWriter.WriteElementString("Address", "2374 Ash St.")
Dim datBirthday As DateTime = #03/05/2000#
objXMLWriter.WriteElementString("birthday", datBirthday.ToString("yyyy-MM-dd"))
Dim intContributions As Integer = 23443
objXMLWriter.WriteElementString("contributions", intContributions.ToString("G"))
Dim blnLocal As Boolean = True
objXMLWriter.WriteElementString("local", blnLocal.ToString())
objXMLWriter.WriteEndElement()
objXMLWriter.WriteEndElement()
objXMLWriter.Flush()
objXMLWriter.Close()
Dim strXMLResult As String
Dim objSR As StreamReader = File.OpenText(strXMLPath)
strXMLResult = objSR.ReadToEnd()
objSR.Close
objSR = Nothing
outResults.innerHTML = "<pre>" & Server.HtmlEncode(strXMLResult)
& "<pre>"
End Sub
</script>
<html>
<body bgcolor="#ffffff">
<div id="outDocURL" runat="server"></div>
<div id="outError" runat="server"> </div>
<div id="outResults" runat="server"></div>
</body>
</html>
Related articles
Related discussion
-
Problem in uploading the image using fck editor in asp.net 2.0
by elizas (13 replies)
-
Tree structures in ASP.NET and SQL Server
by nesreen (29 replies)
-
how to create an xml template
by neelanshu (0 replies)
-
Help required to know about sync of RFID read tag thru Handheld-sample code in .net
by usha@myrf (0 replies)
-
How to query multiple xml files?
by lamin (0 replies)
Related podcasts
-
The One With The Maracas ...
Its the last day of the MVP Summit, the boys dive deep into XML and dynamic language sessions all NDA unfortunately so don't expect them to say anything ... But an interview with Russ Nemhauser uncovers his love for the Mac and concern with some project management methodologies. Brad Abrams gets ...
Events coming up
-
Mar
15
DevWeek 2010
London, United Kingdom
DevWeek is Europe’s leading independent conference for software developers, database professionals and IT architects, and features expert speakers on a wide range of topics, including .NET 4.0, Silverlight 3, WCF 4, Visual Studio 2010, REST, Windows Workflow 4, Thread Synchronization, ASP.NET 4.0, SQL Server 2008 R2, LINQ, Unit Testing, CLR & C# 4.0, .NET Patterns, WPF 4, F#, Windows Azure, ADO.NET, Entity Framework, Debugging, T-SQL Tips & Tricks, and more.
This thread is for discussions of How to write to an XML file.