I am posting this for anyone that runs into a similar issue. This is the whole procedure I created. Keep in mind that this was custom-created to replace the
WIN API function that read a text file, so it may not look like normal XML usage, I may not be a perfect coder, but I am an effective coder, I
get the job done. Note that this allows for a default value to be assigned, something you see often with the WIN API functions.
In the first line of code in the function, Tachufind is the application directory.
Public Function readElement(ByVal path As String, ByVal parentTag As String, ByVal childTag As String, Optional ByVal defaultValue As String = "")
Dim programDataDirectory As String = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) & "\Tachufind"
Dim attrValue As String = ""
Try
checkForDirAndFile(path, programDataDirectory) ' check for the directory and file, create it if it does not exist.
Dim root = XElement.Load(path)
' if no file given, use defaultValue defalts
If parentTag = "" Or root.Element(parentTag) Is Nothing Then
attrValue = defaultValue
Return attrValue
Exit Function
End If
Dim tag As XElement = root.Element(parentTag)
' attrValue = tag.Element(attrName).Value
If tag.Element(childTag) Is Nothing Then
attrValue = defaultValue
Return attrValue
Exit Function
End If
Dim attr As XElement = tag.Element(childTag)
attrValue = tag.Element(childTag).Value
Catch ex As Exception
reportError("Error in " & moduleName & ", readElement : " & Err.Description)
End Try
Return attrValue
End Function
Public Function checkForDirAndFile(ByVal path As String, ByVal programDataDirectory As String)
If Not System.IO.Directory.Exists(programDataDirectory) Then
Directory.CreateDirectory(programDataDirectory)
End If
'Create Element fileName node
If Not File.Exists(path) Then
Dim writer As New XmlTextWriter(path, System.Text.Encoding.UTF8)
writer.WriteStartDocument(True)
writer.Formatting = Formatting.Indented
writer.Indentation = 3
writer.WriteStartElement("root")
writer.WriteEndElement()
writer.WriteEndDocument()
writer.Close()
End If
Return 0
End Function
I also used a replace procedure when I saved any tagNames to make sure that the user could not
enter any characters that would be illegal for the tags: (this is not acomprehensive list, just a few as an example)
pNameOfSearchTable = pNameOfSearchTable.Replace("&", "-")
pNameOfSearchTable = pNameOfSearchTable.Replace("<", "")
pNameOfSearchTable = pNameOfSearchTable.Replace(">", "")
reportError is a module level function I created to display an error, at the beginning of each
module or .vb file I just create a variable like this:
Dim moduleName As String = "ApplicationSettings"
then in the catch statement, I place this code, "readElement" is the name of the function:
reportError("Error in " & moduleName & ", readElement : " & Err.Description)
Function reportError(ByVal text As String)
FrmError.rtbErr.Text = text
FrmError.ShowDialog()
Return 0
End Function
I suppose making it a sub might be a good idea, no need for a return.