Ads 468x60px

Wednesday, December 27, 2017

How to store .NET Project Options as XML in the Project's settings



  1. Right click on your project link, then click on "Properties"
  2. Navigate into "Settings" tab, then add a string parameter. i.e. "ProjectOptions"
  3. The idea is to store multiple Key/Value properties in a single variable using XML format.
  4. Use the function SetProjectOption to store options
        Public Sub SetProjectOption(ByRef ProjectOptions As StringByVal Key As StringByVal Value As String)
            Dim Options As Dictionary(Of StringString)
            Options = Me.GetDictoinary(ProjectOptions)
            If Options.ContainsKey(Key.ToLower) Then
                Options(Key) = Value
            Else
                Options.Add(Key.ToLower, Value)
            End If
     
            Me.storeDictionaryInString(Options, ProjectOptions)
     
        End Sub
  5.  Use the function GetProjectOption to get options values
        Public Function GetProjectOption(ByRef ProjectOptions As StringByVal Key As StringAs String
            Try
                Dim Options As Dictionary(Of StringString)
                Options = Me.GetDictoinary(ProjectOptions)
                Return Options(Key.ToLower)
            Catch ex As Exception
                Return Nothing
            End Try
        End Function
  6. Helper Function:
      Private Function storeDictionaryInString(ByRef dic As Dictionary(Of StringString), ByRef ProjectOption As StringAs String
            Dim doc As New XmlDocument()
            doc.CreateXmlDeclaration("1.0""utf-8"Nothing)
            doc.AppendChild(doc.CreateElement("AllSettings"))
            Dim root As XmlNode = doc.GetElementsByTagName("AllSettings")(0)
            For Each key As String In dic.Keys
                Dim e As XmlElement = doc.CreateElement(key)
                e.InnerText = dic(key)
                root.AppendChild(e)
            Next
            ProjectOption = doc.OuterXml
            Return doc.OuterXml
        End Function
     
        Private Function GetDictoinary(ByRef ProjectOption As StringAs Dictionary(Of StringString)
            Try
                Dim doc As New XmlDocument()
                doc.LoadXml(ProjectOption)
                Dim dic As New Dictionary(Of StringString)
                Dim root As XmlNode = doc.GetElementsByTagName("AllSettings")(0)
                For Each n As XmlNode In root.ChildNodes
                    dic.Add(n.Name.ToLower, n.InnerText)
                Next
     
                Return dic
            Catch ex As Exception
                Return New Dictionary(Of StringString)
            End Try
         
        End Function
Example:
1. Store Options
        Me.SetProjectOption(My.Settings.ProjectOptions, "General.CompanyName""ABC Company Inc")
        Me.SetProjectOption(My.Settings.ProjectOptions, "General.PurchasedDate""27 Dec 2017")
        Me.SetProjectOption(My.Settings.ProjectOptions, "General.OFlAG""True")

2. Read Options
        MsgBox(Me.GetProjectOption(My.Settings.ProjectOptions, "General.OFlAG"))
3. How options stored in the variable:
<?xml version="1.0" encoding="UTF-8"?> <AllSettings> <general.companyname>ABC Company Inc</general.companyname> <general.purchaseddate>27 Dec 2017</general.purchaseddate> <general.oflag>True</general.oflag> </AllSettings>