David,
The approach JH uses is probably better then mine, but, I will include
it nonetheless. Add the following class to your project and you can use the
GetSetting and AddSetting Methods to do what you need it to do. It's not a
perfect solution, you will probably have to customize it. I have tried it
using a webform and it works fine, but, my web server is my local machine. I
don't know how system.io will handle remote user path. Use it or don't,
either way it does provide some example of reading and writing an XML
document.
Jared
'Start Example Page
'Create a new page, copy the code between Start Example Page and End Example
Page into the code behind, add a bunch of text boxes to the form and a
single button
'fill in the boxes, press the button. When you reload the page the values
should persist
Dim Reader As New AppReader
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not IsPostBack Then
LoadSettings()
End If
End Sub
Sub LoadSettings()
Dim Data As String
With Reader
If .Exists Then
For Each Control As Control In Me.Controls
If TypeOf Control Is HtmlForm Then
For Each c As Control In CType(Control,
HtmlForm).Controls
If TypeOf c Is TextBox Then
CType(c, TextBox).Text = ""
Data = .GetSetting(c.ID)
CType(c, TextBox).Text = Data
End If
Next
End If
Next
End If
End With
End Sub
Sub SaveSettings()
Dim Data As String
With Reader
For Each Control As Control In Me.Controls
If TypeOf Control Is HtmlForm Then
For Each c As Control In CType(Control,
HtmlForm).Controls
If TypeOf c Is TextBox Then
.AddSetting(c.ID, CType(c, TextBox).Text)
End If
Next
End If
Next
End With
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
SaveSettings()
End Sub
'End Example Page
'Start AppReader Class
Imports System.Xml
Imports System.Reflection
Public Class AppReader
Private xDoc As New XmlDocument, xNodes As XmlNodeList
Private mAppName As String, xNode, xRoot As XmlNode
Private mFilePath As String
Friend ReadOnly Property Exists() As Boolean
Get
Return System.IO.File.Exists(mFilePath)
End Get
End Property
Public Sub New()
mFilePath =
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
mAppName = [Assembly].GetExecutingAssembly().GetName.Name.ToString
Me.mFilePath += "\" & mAppName & ".settings"
If System.IO.File.Exists(Me.mFilePath) Then
LoadFile()
Else
CreateFile()
End If
End Sub
Private Sub LoadFile()
With xDoc
.Load(mFilePath)
xRoot = xDoc.DocumentElement
End With
End Sub
Private Sub CreateFile()
With xDoc
Dim Dec As XmlDeclaration = .CreateXmlDeclaration("1.0",
Nothing, Nothing)
xRoot = .CreateElement("Settings")
.AppendChild(xRoot)
.PrependChild(Dec)
Try
.Save(mFilePath)
Catch ioex As System.IO.IOException
End Try
End With
End Sub
Friend Function GetSetting(ByVal Key As String) As Object
Try
xNode = xDoc.SelectSingleNode("//Add[@Key='" & Key & "']")
Return xNode.Attributes("Value").InnerText.TrimEnd
Catch ex As Exception
Return vbNullString
End Try
End Function
Friend Function AddSetting(ByVal Key As String, ByVal Value As String)
As Boolean
Dim xKey, xValue As XmlAttribute
If Key.Length AndAlso Value.Length > 0 Then
With xDoc
Try
xNode = xDoc.SelectSingleNode("//Add[@Key='" & Key &
"']")
Catch ex As Exception
End Try
If xNode Is Nothing Then
xNode = .CreateElement("Add")
xKey = .CreateAttribute("Key")
xKey.InnerText = Key
xNode.Attributes.Append(xKey)
xValue = .CreateAttribute("Value")
xValue.InnerText = Value
xNode.Attributes.Append(xValue)
xRoot.AppendChild(xNode)
Else
xNode.Attributes("Key").InnerText = Key
xNode.Attributes("Value").InnerText = Value
End If
Try
.Save(mFilePath)
Catch ex As Exception
End Try
End With
End If
End Function
End Class
'End AppReader Class
[quoted text, click to view] "David Hearn" <david.hearn@stagparkway.com> wrote in message
news:%239EcanpXEHA.2520@TK2MSFTNGP12.phx.gbl...
>I have an ASP.NET app that I am writing using VB.NET and I want to use an
>XML file to store configuration settings in sort of like the way I used to
>use an .INI file for in Windows apps. When the user opens a page, it needs
>to go read certain settings out of the file and fill textboxes and other
>form fields with the data from the XML file. If the user makes any changes
>on the form, it needs to write back to the XML file to store the new
>settings.
>
> Any help with this would be greatly appreciated!
>