I've already changed to the second way. Now I need to get down to some serious bur tedious coding to finish of the toolkit part of the application again with the new storage system.
Seems to work really well.
My core XML object is:
Imports System.Xml
Public Class RootXMLStore
Private _document As New XmlDocument
Private _filename As String
Public Enum ContentType
CharacterRace
CharacterGuild
CharacterClass
CharacterSkill
CharacterSpell
Monster
Item
Spell
Trap
End Enum
Public Sub New(ByVal filename As String)
_filename = filename
If Load() = False Then
CreateDocStructure()
End If
End Sub
Private ReadOnly Property Location(ByVal type As ContentType) As String
Get
Select Case type
Case ContentType.CharacterClass
Return "root/mob/classes"
Case ContentType.CharacterGuild
Return "root/mob/guilds"
Case ContentType.CharacterRace
Return "root/mob/races"
Case ContentType.CharacterSkill
Return "root/mob/skills"
Case ContentType.CharacterSpell
Return "root/mob/spells"
Case ContentType.Item
Return "root/environment/items"
Case ContentType.Monster
Return "root/mob/monsters"
Case ContentType.Spell
Return "root/mob/spells"
Case ContentType.Trap
Return "root/environment/traps"
Case Else
Return ""
End Select
End Get
End Property
Public ReadOnly Property GetNode(ByVal type As ContentType) As XmlNode
Get
Return _document.SelectSingleNode(Location(type))
End Get
End Property
Public Property Document() As XmlDocument
Get
Return _document
End Get
Set(ByVal value As XmlDocument)
_document = value
End Set
End Property
Public ReadOnly Property Tree(ByVal Type As ContentType) As String()
Get
Dim list As XmlNode = _document.SelectSingleNode(Location(Type))
If list.ChildNodes.Count > 0 Then
Dim result(list.ChildNodes.Count - 1) As String
Dim lp As Integer = 0
For Each node As XmlNode In list.ChildNodes
result(lp) = node.Name
lp += 1
Next
Return result
Else
Dim result() As String = {}
Return result
End If
End Get
End Property
Public Function Save() As Boolean
Try
_document.Save(_filename)
Catch ex As Exception
Return False
End Try
Return True
End Function
Public Function Load() As Boolean
Try
_document.Load(_filename)
Catch ex As Exception
Return False
End Try
Return True
End Function
Private Sub CreateDocStructure()
_document = New XmlDocument
' Create XML the declaration
Dim XmlDec As XmlDeclaration = _document.CreateXmlDeclaration("1.0", "utf-8", Nothing)
' Create the root element
Dim rootNode As XmlElement = _document.CreateElement("root")
' Add the declaration and root element to the document
_document.InsertBefore(XmlDec, _document.DocumentElement)
_document.AppendChild(rootNode)
' Create the character node and instert into document
' This node is for all character aspects
Dim characterNode As XmlElement = _document.CreateElement("mob")
rootNode.AppendChild(characterNode)
' Create the Races node and insert into document
Dim racesNode As XmlElement = _document.CreateElement("races")
characterNode.AppendChild(racesNode)
' Create the guilds node and insert into document
Dim guildsNode As XmlElement = _document.CreateElement("guilds")
characterNode.AppendChild(guildsNode)
' Create the classes node and insert into document
Dim classesNode As XmlElement = _document.CreateElement("classes")
characterNode.AppendChild(classesNode)
' Create the skills node and insert into document
Dim skillsNode As XmlElement = _document.CreateElement("skills")
characterNode.AppendChild(skillsNode)
' Create the spells node and insert into document
Dim spellsNode As XmlElement = _document.CreateElement("spells")
characterNode.AppendChild(spellsNode)
' Create the monsters node and insert into document
Dim monstersNode As XmlElement = _document.CreateElement("monsters")
characterNode.AppendChild(monstersNode)
' Create the environment node and insert into document
' This has all things like items included
Dim environmentNode As XmlElement = _document.CreateElement("environment")
rootNode.AppendChild(environmentNode)
' Create the items node and insert into documents
Dim itemsNode As XmlElement = _document.CreateElement("items")
environmentNode.AppendChild(itemsNode)
End Sub
End Class
The Tree Property means I can populate list/combo boxes on the forum without having to write any extra code:
guildNameComboBox.Items.Clear()
guildNameComboBox.Items.AddRange(_store.Tree(RootXMLStore.ContentType.CharacterGuild))
And the Objects themselves can be populated from the store or update the XML document with data, e.g.:
Imports System.Xml
Public Class characterGuild
Private Const STR_Desc As String = "desc"
Private Const STR_InitClass As String = "initClass"
Private _root As XmlNode
Private _store As XmlDocument
Private _name As String = ""
Private _description As String = ""
Private _initialClass As String = ""
Public Sub New(ByVal xmlStore As RootXMLStore)
_root = xmlStore.GetNode(RootXMLStore.ContentType.CharacterGuild)
_store = xmlStore.Document
End Sub
Public Sub New(ByVal xmlStore As XmlDocument, ByVal ID As String)
_name = ID
Load()
End Sub
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
Public Property Description() As String
Get
Return _description
End Get
Set(ByVal value As String)
_description = value
End Set
End Property
Public Property InitialClass() As String
Get
Return _initialClass
End Get
Set(ByVal value As String)
_initialClass = value
End Set
End Property
Public Function Load() As Boolean
If _name = "" Then
Return False
Else
Dim node As XmlNode = _root.SelectSingleNode(Name)
If IsNothing(node) = False Then
Dim subEle As XmlElement
subEle = node.SelectSingleNode(STR_Desc)
_description = subEle.InnerText
subEle = node.SelectSingleNode(STR_InitClass)
_initialClass = subEle.InnerText
Return True
Else
Return False
End If
End If
End Function
Public Sub Save()
Dim node As XmlNode
Dim replaceFlag As Boolean = False
node = _root.SelectSingleNode(Name)
If IsNothing(node) = False Then
replaceFlag = True
End If
Dim item As XmlElement = _store.CreateElement(_name)
Dim subEle As XmlElement = _store.CreateElement(STR_Desc)
subEle.InnerText = _description
item.AppendChild(subEle)
subEle = _store.CreateElement(STR_InitClass)
subEle.InnerText = _initialClass
item.AppendChild(subEle)
If replaceFlag = True Then
_root.ReplaceChild(item, node)
Else
_root.AppendChild(item)
End If
End Sub
End Class
Adding a Guild using the toolkit produces the XML file:
<?xml version="1.0" encoding="utf-8"?>
<root>
<mob>
<races />
<guilds>
<Street>
<desc>Test</desc>
<initClass>Class</initClass>
</Street>
</guilds>
<classes />
<skills />
<spells />
<monsters />
</mob>
<environment>
<items />
</environment>
</root>
----
"What is this talk of 'release'? Klingons do not'release' software. It escapes leaving a bloody trail of developers and quality assurance people in its wake!"