Topic: CXMLINI Class
CXMLINI Class: A Replacement for INI Files in VB.NET
In VB6, one way to store data has long been the elderly INI file. Since Windows 95, Microsoft has pushed hard to get rid of the INI file, and move that data into the registry. Many developers are wary of that approach, fearful of contributing to the "clutter" of the registry. So the INI file has survived despite Microsoft's best efforts.
And there are other advantages to an INI file. In Rogue, it holds video settings, key mappings, and more. It's much quicker to open Notepad and change an INI file than it is to modify registry entries. But INI support in .NET falls outside of managed code...so if you want a "real" .NET way of doing this, you need to go to XML.
Let's look at the video section of Rogue's INI file:
[Video]
Height=768
Width=1024
Windowed=1
TripleBuffer=0And the XML file for the same entries:
<sections>
<section name="video">
<item key="Width" value="800" />
<item key="Height" value="600" />
<item key="Windowed" value="1" />
<item key="TripleBuffer" value="0" />
</section>
</sections>The structure is easy enough to follow, and is modelled after the INI file structure (different sections, with numerous key/value sets). The hard part of dealing with the XML itself will be handled by the xmlDocument object.
Let's first take a look at the class declarations and the New procedure(s):
Public Class CXMLINI
Private doc As New Xml.XmlDocument
Private xmlfile As String
Public Sub New(ByVal Filename As String)
' open xml file for use
Try
doc.Load(Filename)
xmlfile = Filename
Catch ex As Exception
End Try
End Sub
Public Sub New()
' open default xml file for use
Dim s As String
s = System.AppDomain.CurrentDomain.BaseDirectory
s &= Application.ProductName
s &= ".xml"
Try
doc.Load(s)
xmlfile = s
Catch ex As Exception
End Try
End Subdoc is the xmlDocument object for the class, which we'll maintain until the class is destroyed. This object stores the contents of the xml file we load in memory, where we can almost treat it like a dataset.
Notice that I've overloaded the New procedure. When given a filename, it opens that xml file. Otherwise, it defaults to Application Name.xml. In either case, if the file doesn't exist, it catches the error and goes on, but remembers the file name that it was supposed to use (for later use).