XML wie Ini Datei

Gebrauch einer XML-Datei wie eine INI zum Abspeichern von Einstellungen.

Beispiel:

private Settings As XML_Settings

Settings = New XML_Settings(APPPath & "Settings.ini")
LESEN
Dummy = Settings.Read("Suchen", "Lastword")
SCHREIBEN
Settings.Write("Suchen", "Lastword", Dummy)

  • Option Strict On
  • Option Explicit On
  • Imports System.Xml
  • Public Class XML_Settings
  •     private mFilename As string = ""
  •     private Erlaubt As string = "-_.äöüabcdefghijklmnopqrstuvwxyz0123456789"
  •     private TXT_Builder As System.Text.StringBuilder = Nothing
  •     private Const TrueValue As string = "True"
  •     private Const FalseValue As string = "False"
  •     private xmlDoc As XmlDocument
  •     private File_Exists As Boolean
  •     Public Function Reset() As Boolean
  •         Reset = False
  •         Try
  •             If mFilename <> "" AndAlso Dir(mFilename) <> "" Then
  •                 xmlDoc = Nothing
  •                 Application.DoEvents()
  •                 Kill(mFilename)
  •                 Application.DoEvents()
  •                 Me.Open()
  •             Else
  •                 xmlDoc = Nothing
  •             End If
  •             Reset = True
  •         Catch : End Try
  •     End Function
  •     Public Function Load_FormSize(ByVal Form As Form) As Boolean
  •         Load_FormSize = False
  •         Try
  •             Dim Section As string = Make_Correct_ISO_String(Form.Name)
  •             Dim Dummy As string = ""
  •             Form.WindowState = FormWindowState.Normal
  •             Dummy = Me.Read(Section, "Left")
  •             If Dummy <> "" AndAlso IsNumeric(Dummy) _
  •             AndAlso CInt(Dummy) > 0 Then Form.Left = CInt(Dummy)
  •             Dummy = Me.Read(Section, "Top")
  •             If Dummy <> "" AndAlso IsNumeric(Dummy) _
  •             AndAlso CInt(Dummy) > 0 Then Form.Top = CInt(Dummy)
  •             Dummy = Me.Read(Section, "Width")
  •             If Dummy <> "" AndAlso IsNumeric(Dummy) _
  •             AndAlso CInt(Dummy) > 0 Then Form.Width = CInt(Dummy)
  •             Dummy = Me.Read(Section, "Height")
  •             If Dummy <> "" AndAlso IsNumeric(Dummy) _
  •             AndAlso CInt(Dummy) > 0 Then Form.Height = CInt(Dummy)
  •             Load_FormSize = True
  •         Catch : End Try
  •     End Function
  •     Public Function Save_FormSize(ByVal Form As Form) As Boolean
  •         Save_FormSize = False
  •         Try
  •             If Form.WindowState = FormWindowState.Normal Then
  •                 Dim Section As string = Make_Correct_ISO_String(Form.Name)
  •                 Me.Write(Section, "Left", CStr(Form.Left), True)
  •                 Me.Write(Section, "Top", CStr(Form.Top), True)
  •                 Me.Write(Section, "Width", CStr(Form.Width), True)
  •                 Me.Write(Section, "Height", CStr(Form.Height), True)
  •             End If
  •             Save_FormSize = True
  •         Catch : End Try
  •     End Function
  •     Public Function Make_Correct_ISO_String(ByVal Wrong_Section As string) As string
  •         '    entfehrnen aller zeichen außer a-z, A-Z und 0-9
  •         Make_Correct_ISO_String = ""
  •         Try
  •             Dim i As Integer, CNT As Integer, mChar As string = ""
  •             If Not Wrong_Section Is Nothing Then
  •                 CNT = Wrong_Section.Length - 1
  •             Else
  •                 CNT = -1
  •             End If
  •             TXT_Builder = New System.Text.StringBuilder
  •             For i = 0 To CNT
  •                 mChar = Wrong_Section.Substring(i, 1)
  •                 If Erlaubt.IndexOf(LCase(mChar)) <> -1 Then
  •                     TXT_Builder.Append(mChar)
  •                 End If
  •             Next
  •             Make_Correct_ISO_String = TXT_Builder.ToString
  •             If Make_Correct_ISO_String <> "" Then
  •                 If IsNumeric(Make_Correct_ISO_String.Substring(0, 1)) Then
  •                     Make_Correct_ISO_String = "O" & Make_Correct_ISO_String
  •                 End If
  •             Else
  •                 Make_Correct_ISO_String = "O"
  •             End If
  •         Catch : End Try
  •     End Function
  •     Public Function Read_Boolean(ByVal Section As string, _
  •     ByVal Key As string, _
  •     Optional ByVal Default_Value As Boolean = False) As Boolean
  •         Read_Boolean = Default_Value
  •         Try
  •             Dim Ret As string
  •             Ret = Read(Section, Key)
  •             If Ret = TrueValue Then
  •                 Read_Boolean = True
  •             Else
  •                 If Ret <> "" Then
  •                     Read_Boolean = False
  •                 End If
  •             End If
  •         Catch : End Try
  •     End Function
  •     Public Function Write_Boolean(ByVal Section As string, _
  •     ByVal Key As string, _
  •     ByVal Value As Boolean, _
  •     Optional ByVal Do_Flush As Boolean = True) As Boolean
  •         Write_Boolean = False
  •         Try
  •             Dim mVal As string = ""
  •             If Value Then
  •                 mVal = TrueValue
  •             Else
  •                 mVal = FalseValue
  •             End If
  •             Write_Boolean = Write(Section, Key, mVal, Do_Flush)
  •         Catch : End Try
  •     End Function
  •     Public Function Delete_Key(ByVal Section As string, ByVal Key As string, _
  •             Optional ByVal Do_Flush As Boolean = True) As Boolean
  •         Delete_Key = False
  •         Try
  •             Dim xSection As string = Make_Correct_ISO_String(Section)
  •             Dim Section_Node As XmlNode, Key_Node As XmlNode
  •             Dim xKey As string = Me.Make_Correct_ISO_String(Key)
  •             Dim Root_Node As XmlNode
  •             Root_Node = xmlDoc.SelectSingleNode("XML")  'section:
  •             Section_Node = Root_Node.SelectSingleNode(xSection)
  •             If Not Section_Node Is Nothing Then
  •                 Key_Node = Section_Node.SelectSingleNode(xKey)
  •                 If Not Key_Node Is Nothing Then
  •                     Key_Node.RemoveAll()
  •                 End If
  •             End If
  •             If Do_Flush Then Me.Flush()
  •             Key_Node = Nothing
  •             Section_Node = Nothing
  •             Root_Node = Nothing
  •             Delete_Key = True
  •         Catch : End Try
  •     End Function
  •     Public Function Delete_Section(ByVal Section As string, Optional ByVal Do_Flush As Boolean = True) As Boolean
  •         Delete_Section = False
  •         Try
  •             Dim xSection As string = Make_Correct_ISO_String(Section)
  •             Dim Section_Node As XmlNode
  •             Dim Root_Node As XmlNode
  •             Root_Node = xmlDoc.SelectSingleNode("XML")
  •             'section:
  •             Section_Node = Root_Node.SelectSingleNode(xSection)
  •             If Not Section_Node Is Nothing Then
  •                 Section_Node.RemoveAll()
  •             End If
  •             If Do_Flush Then Me.Flush()
  •             Section_Node = Nothing
  •             Root_Node = Nothing
  •             Delete_Section = True
  •         Catch : End Try
  •     End Function
  •     Public Function Write(ByVal Section As string, ByVal Key As string, _
  •     ByVal Value As string, _
  •     Optional ByVal Do_Flush As Boolean = False) As Boolean
  •         Write = False
  •         Try
  •             Dim xSection As string = Make_Correct_ISO_String(Section)
  •             Dim xKey As string = Make_Correct_ISO_String(Key)
  •             Dim Section_Node As XmlNode, Key_Node As XmlNode
  •             Dim Root_Node As XmlNode
  •             Root_Node = xmlDoc.SelectSingleNode("XML")
  •             'section:
  •             Section_Node = Root_Node.SelectSingleNode(xSection)
  •             If Section_Node Is Nothing Then
  •                 Section_Node = xmlDoc.CreateElement(xSection)
  •                 Root_Node.AppendChild(Section_Node)
  •                 xmlDoc.AppendChild(Root_Node)
  •                 Section_Node = Root_Node.SelectSingleNode(xSection)
  •             End If
  •             ' key
  •             Key_Node = Section_Node.SelectSingleNode(xKey)
  •             If Key_Node Is Nothing Then
  •                 Key_Node = xmlDoc.CreateElement(xKey)
  •                 Section_Node.AppendChild(Key_Node)
  •                 Root_Node.AppendChild(Section_Node)
  •                 Key_Node = Section_Node.SelectSingleNode(xKey)
  •             End If
  •             'value:
  •             If Value Is Nothing Then Value = ""
  •             Key_Node.InnerText = Value.Trim
  •             Section_Node.AppendChild(Key_Node)
  •             Root_Node.AppendChild(Section_Node)
  •             xmlDoc.AppendChild(Root_Node)
  •             If Do_Flush Then Me.Flush()
  •             Key_Node = Nothing
  •             Section_Node = Nothing
  •             Root_Node = Nothing
  •             Write = True
  •         Catch : End Try
  •     End Function
  •     Public Function Get_Size() As Long
  •         Get_Size = -1
  •         Try
  •             Flush()
  •             Dim FI_XML As New System.IO.FileInfo(mFilename)
  •             Get_Size = FI_XML.Length
  •             FI_XML = Nothing
  •         Catch : End Try
  •     End Function
  •     Public Function Read(ByVal Section As string, _
  •             ByVal Key As string) As string
  •         Read = ""
  •         Try
  •             Dim xSection As string = Make_Correct_ISO_String(Section)
  •             Dim xKey As string = Make_Correct_ISO_String(Key)
  •             Dim xpNav As XPath.XPathNavigator
  •             Dim xNi_XML As XPath.XPathNodeIterator
  •             '
  •             xpNav = xmlDoc.CreateNavigator
  •             xNi_XML = xpNav.Select("XML/" & xSection & "/" & xKey)
  •             If xNi_XML.MoveNext Then
  •                 Read = xNi_XML.Current.Value.Trim
  •             End If
  •             xpNav = Nothing
  •         Catch : End Try
  •     End Function
  •     Public Function Read_Section(ByVal Section As string, _
  • ByRef Keys() As string, _
  • ByRef Values() As string) As Integer
  •         Read_Section = 0 'keine eintrage:
  •         Try
  •             Dim xSection As string = Make_Correct_ISO_String(Section)
  •             Dim xNi_KEY As XPath.XPathNodeIterator
  •             Dim Key As string = "", Value As string = ""
  •             Dim xpNav As XPath.XPathNavigator
  •             Dim xNi_XML As XPath.XPathNodeIterator
  •             Dim Keys_redim As New Fast_Array_CLS(Of string)
  •             Dim Values_redim As New Fast_Array_CLS(Of string)
  •             xpNav = xmlDoc.CreateNavigator
  •             xNi_XML = xpNav.Select("XML/" & xSection)
  •             If xNi_XML.MoveNext Then
  •                 xNi_KEY = xNi_XML.Current.SelectChildren(XPath.XPathNodeType.Element)
  •                 Do While xNi_KEY.MoveNext 'key lesen:
  •                     Key = xNi_KEY.Current.Name.Trim 'value lesen:
  •                     Value = xNi_KEY.Current.Value.Trim
  •                     If Key <> "" AndAlso Value <> "" Then
  •                         Keys_redim.Redim_(Keys, Read_Section)
  •                         Values_redim.Redim_(Values, Read_Section)
  •                         Keys(Read_Section) = Key
  •                         Values(Read_Section) = Value
  •                         Read_Section += 1
  •                     End If
  •                 Loop
  •                 Keys_redim.Cut(Keys)
  •                 Values_redim.Cut(Values)
  •             End If
  •             xNi_KEY = Nothing
  •             xNi_XML = Nothing
  •             xpNav = Nothing
  •         Catch : End Try
  •     End Function
  •     private Sub Open()
  •         Try
  •             Dim xComment As XmlComment
  •             Dim Root_Node As XmlNode
  •             If mFilename = "" Then Exit Sub
  •             xmlDoc = New XmlDocument
  •             If Dir(mFilename) <> "" Then
  •                 File_Exists = True
  •                 Try
  •                     xmlDoc.Load(mFilename)
  •                 Catch
  •                     Me.Reset()
  •                     File_Exists = False
  •                 End Try
  •             Else
  •                 Me.Reset()
  •                 File_Exists = False
  •                 xmlDoc = New Xml.XmlDocument
  •             End If
  •             If Not File_Exists Then
  •                 xmlDoc.AppendChild(xmlDoc.CreateXmlDeclaration("1.0", _
  •                                 System.Text.Encoding.UTF8.HeaderName, Nothing))
  •                 Root_Node = xmlDoc.CreateElement("XML")
  •                 xmlDoc.AppendChild(Root_Node)
  •                 xComment = xmlDoc.CreateComment("File: """ & mFilename & """")
  •                 xmlDoc.AppendChild(xComment)
  •             Else
  •                 Root_Node = xmlDoc.SelectSingleNode("XML")
  •                 If Root_Node Is Nothing Then
  •                     Root_Node = xmlDoc.CreateElement("XML")
  •                     xmlDoc.AppendChild(Root_Node)
  •                     xComment = xmlDoc.CreateComment("File: """ & mFilename & """")
  •                     xmlDoc.AppendChild(xComment)
  •                 End If
  •             End If
  •         Catch : End Try
  •     End Sub
  •     Public Sub New(ByVal Filename As string)
  •         Try
  •             mFilename = Filename
  •             Open()
  •         Catch : End Try
  •     End Sub
  •     Public Function Flush() As Boolean
  •         Flush = False
  •         Try
  •             xmlDoc.PreserveWhitespace = False
  •             xmlDoc.Save(mFilename)
  •             Flush = True
  •         Catch : End Try
  •     End Function
  •     Public Function Rename(ByVal New_FullName As string) As Boolean
  •         Rename = False
  •         Try
  •             Flush()
  •             xmlDoc = Nothing
  •             System.IO.File.Move(mFilename, New_FullName)
  •             mFilename = New_FullName
  •             Me.Open()
  •             Rename = True
  •         Catch : End Try
  •     End Function
  •     Protected overrides Sub Finalize()
  •         Try
  •             Flush()
  •             xmlDoc = Nothing
  •             MyBase.Finalize()
  •         Catch : End Try
  •     End Sub
  •     Public Function Get_APPPath() As string
  •         Get_APPPath = ""
  •         Try
  •             Get_APPPath = My.Application.Info.DirectoryPath
  •             If Right(Get_APPPath, 1) <> "\" Then Get_APPPath = Get_APPPath + "\'"
  •         Catch : End Try
  •     End Function
  •     Public Function Key_exists(ByVal Section As string, _
  •             ByVal Key As string) As Boolean
  •         Key_exists = False
  •         Try
  •             Dim xSection As string = Make_Correct_ISO_String(Section)
  •             Dim xKey As string = Make_Correct_ISO_String(Key)
  •             Dim xpNav As XPath.XPathNavigator
  •             Dim xNi_XML As XPath.XPathNodeIterator
  •             xpNav = xmlDoc.CreateNavigator
  •             xNi_XML = xpNav.Select("XML/" & xSection & "/" & xKey)
  •             If xNi_XML.MoveNext Then
  •                 Key_exists = True
  •             End If
  •             xpNav = Nothing
  •             xNi_XML = Nothing
  •         Catch : End Try
  •     End Function
  •     Public Property FileName() As string
  •         Get
  •             FileName = ""
  •             Try
  •                 Return mFilename
  •             Catch : End Try
  •         End Get
  •         Set(ByVal value As string)
  •             Try
  •                 mFilename = value
  •                 Open()
  •             Catch : End Try
  •         End Set
  •     End Property
  •     private Class Fast_Array_CLS(Of T)
  •         private mIntervall As Integer = 1000, Real_Size As Integer = -1
  •         private Cut_To As Integer = -1
  •         Friend Sub Redim_(ByRef xArray() As T, ByVal Redim_To As Integer)
  •             Try
  •                 If Redim_To > Real_Size Then
  •                     Real_Size += mIntervall - 1
  •                     ReDim Preserve xArray(0 To Real_Size)
  •                 End If
  •                 Cut_To = Redim_To
  •             Catch : End Try
  •         End Sub
  •         Friend Sub Cut(ByRef xArray() As T)
  •             Try
  •                 If Not Cut_To = -1 Then ReDim Preserve xArray(0 To Cut_To)
  •                 Real_Size = Cut_To
  •             Catch : End Try
  •         End Sub
  •     End Class
  • End Class