can anyone show me how i can write an xml file kind of like an ini file, and also read from it? please bear in mind that i don't know all that much about how xml files work/are structured. thanks. :)
Printable View
can anyone show me how i can write an xml file kind of like an ini file, and also read from it? please bear in mind that i don't know all that much about how xml files work/are structured. thanks. :)
if you need this later then you will get it from me (cus i need to do this for myself)
but if someone has time and can do this, even better :)
Take a look at this thread: http://www.vbforums.com/showthread.p...hreadid=147115
It basicly gives you the idea on how to put a class or Structure (formerly known as type in VB6) in a XML file... and read from it to a class or structure..
Or even an array!
hmm, ok, i'll give it a go, thanks :)
This an old thread, but it is the same thing as I need to do. I have worked with reading and writing XML before (for data tables) but not for config/ini type files. I was wondering if there is an easier way than I have been using.
The link above is dead (probably got archived).
I guess I just got lucky. I found this on the Tutorials thread that was just posted a few minutes ago.
For anyone doing searches in the archived threads, here it is:
http://www.devx.com/dotnet/Article/7008
Upgrade Your INI Files to XML with .NET
As you upgrade your .NET applications, upgrade those legacy INI files as well, and save yourself some headaches.
here's a little class i wrote awhile ago to do something similar...
VB Code:
'XMLSettings Class 'Date: February 10th, 2003 'Programmer: Jon Dick 'Website: [url]http://www.Net-Lutions.com[/url] 'Email: mailto:[email protected] ' 'Description: ' I designed this class to easily manage program settings in all of my windows and web applications. There are 'other INI XML file classes out there, but their code is more complicated though quite possibly more efficient. 'My goal was to make a simple class for this, no frills, just something that works, so here it is. At the same 'time, i learned a bit more about working with Datasets and XML, and so that was accomplished. 'Hopefully someone finds this useful, if not for the actual use, then for at least learning how to work with datasets 'and XML in a simple, quick, and clean fashion. Enjoy it! ' 'Oh yeah, go ahead and use this code for whatever you like. It took me an hour at most to make, so Use if freely! Public Class XMLSettings Public Shared Function Write(ByVal strFileName As String, ByVal strSetting As String, ByVal strValue As String) As Boolean 'This Function Takes a setting and it's corresponding value and writes it to the specified XML file Dim dsSettings As DataSet dsSettings = New DataSet() If System.IO.File.Exists(strFileName) Then 'If a file already exists, load into the dataset dsSettings.ReadXml(strFileName) Else 'No file exists, so Create a new dataset dsSettings.DataSetName = "XML_INI" 'Create Table in the dataset dsSettings.Tables.Add("Settings") 'Create a new row, and add it to the table Dim newRow As DataRow newRow = dsSettings.Tables(0).NewRow dsSettings.Tables(0).Rows.Add(newRow) 'Dispose of the datarow newRow = Nothing End If 'Now we are ready to make changes to the setting and its value 'Check to see if the setting we want exists yet or not If dsSettings.Tables(0).Columns(strSetting) Is Nothing Then 'Setting doesn't exist, so create a new column for it dsSettings.Tables(0).Columns.Add(strSetting) End If 'Set the column value dsSettings.Tables(0).Rows(0)(strSetting) = strValue 'Write the new changes to the xml file dsSettings.WriteXml(strFileName) 'Dispose of the Dataset object immediately dsSettings.Dispose() End Function Public Shared Function Read(ByVal strFileName As String, ByVal strSetting As String) As String 'This Function Reads A setting, returning its value (if it exists) from a specified XML File Dim dsSettings As DataSet dsSettings = New DataSet() 'Try Reading the XML File. If it doesn't exist, return a setting value of nothing, since the setting 'obviously also does not exist Try dsSettings.ReadXml(strFileName) Catch Read = "" End Try 'If the file does exist, attempt to Read the Column (Setting) Value 'NOTE: The settings are always saved in the first row of the first table in the dataset Try Read = dsSettings.Tables(0).Rows(0)(strSetting) Catch Read = "" End Try 'Dispose Immediately of the dataset dsSettings.Dispose() End Function Public Shared Function Delete(ByVal strFileName As String, ByVal strSetting As String) As Boolean 'This Function Deletes A setting, returning a success boolean value Dim dsSettings As DataSet dsSettings = New DataSet() 'Try Reading the XML File. If it doesn't exist, return a setting value of nothing, since the setting 'obviously also does not exist Try dsSettings.ReadXml(strFileName) Catch 'File doesn't exist, nothing to delete Delete = False Exit Function End Try 'File Exists, so remove the column (setting) if it exists Try dsSettings.Tables(0).Columns.Remove(strSetting) 'Write Back to XML File dsSettings.WriteXml(strFileName) Delete = True Catch 'Column Doesn't Exist Delete = False End Try 'Dispose Immediately of the dataset dsSettings.Dispose() End Function End Class
It works pretty good for what it's use is... i use it to store program settings....
Or you could create a Serializable class that encapsulates the settings you want to persist - eg.
Then in your application you can do the following :PHP Code:<Serializable()> Public Class MySettings
Private m_Setting1 As String
Private m_Setting2 As Integer
Public Property Setting1() As String
Get
Return m_Setting1
End Get
Set(ByVal Value As String)
m_Setting1 = Value
End Set
End Property
Public Property Setting2() As Integer
Get
Return m_Setting2
End Get
Set(ByVal Value As Integer)
m_Setting2 = Value
End Set
End Property
End Class
This will create a XML file which is then Deserializable (see the Help). Once you have deserialized the object you can just look at its properties.PHP Code:Imports System.Runtime.Serialization.Formatters.Soap
Imports System.IO
Dim oSerializer As New SoapFormatter()
Dim oStream As Stream
Dim oSettings as MySettings 'an instance of your settings class
oSettings.Setting1 = "Some text"
oSettings.Setting2 = 123
oStream = File.Create("C:\MySettings.xml")
oSerializer.Serialize(oStream, oSettings)
oStream.Close()
It is also possible to create a Binary file if thats preferable.