Wednesday, February 1, 2012

Read, Add, Update, Modify, or Delete lines from XML app.config file VB.NET

I setup my program with a custom app.config file. The file is used store most application user settings.
I use this code to read, update, add, and delete lines as needed.

My application app.config file
*******************

Imports System.Configuration
Imports System.Xml
Imports System.Xml.Linq
Imports System.Xml.Linq.Extensions



''' <summary>
''' AppConfigFileSettings: This class is used to Change the
''' AppConfigs Parameters at runtime through User Interface
''' </summary>
''' <remarks></remarks>


Public Class AppConfigFileSettings

    Public Shared Sub addmenuitem(ByVal menuname As String, ByVal menuvisible As Integer, ByVal menutext As String, ByVal menutype As Integer, ByVal menuurl As String)

        Dim theXMLSource As String = (ApppPath & "\menu.xml")
        Dim document As XDocument = XDocument.Load(theXMLSource)
        Dim attrAndValue As XElement =
            <link>
                <name><%= menuname %></name>
                <visible><%= menuvisible %></visible>
                <text><%= menutext %></text>
                <type><%= menutype %></type>
                <url><%= menuurl %></url>
            </link>
        document.Root.Add(attrAndValue)
        document.Save(theXMLSource)

        MsgBox(menuname & " " & menuvisible & " " & menutext & " " & menutype & " " & menuurl)


    End Sub

    Public Shared Sub deletemenuitem(ByVal menuname As String)

        'this is not being use. This deletes a node without a attribute value

        Dim theXMLSource As String = (ApppPath & "\menu.xml")
        Dim document As New XmlDocument

        document.Load(theXMLSource)

        For Each myNode As XmlNode In document.DocumentElement.FirstChild.ChildNodes
            If myNode.FirstChild.InnerText.ToString = menuname Then
                'Do While myNode.HasChildNodes
                'myNode.ParentNode.RemoveAll()
                myNode.ParentNode.RemoveAll()

                'Loop
            End If

        Next
        document.Save(theXMLSource)

        MsgBox(menuname)


    End Sub
    Public Shared Sub removemenuitem(ByVal menuname As String)

        'this is not being use. This deletes a node without a attribute value

        Dim theXMLSource As String = (ApppPath & "\menu.xml")
        Dim document As New XmlDocument
        Dim txtname As String


        document.Load(theXMLSource)

        For Each myNode As XmlNode In document.DocumentElement

            txtname = myNode.Attributes("value").InnerText

            ' MsgBox(txtname)
            If txtname = menuname Then
                myNode.ParentNode.RemoveChild(myNode)
            End If
        Next

        document.Save(theXMLSource)
        MsgBox(menuname)


    End Sub
    Public Shared Sub UpdateAppSettings(ByVal KeyName As String, ByVal KeyValue As String)

        'MsgBox(KeyName & " " & KeyValue)

        '  AppDomain.CurrentDomain.SetupInformation.ConfigurationFile
        ' This will get the app.config file path from Current application Domain
        Dim XmlDoc As New XmlDocument()
        ' Load XML Document

        'MsgBox(ApppPath & "\vbbterm.config")
        XmlDoc.Load(ApppPath & "\vbbterm.txt")

        ' Navigate Each XML Element of app.Config file
        'MsgBox(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile.ToString)
        For Each xElement As XmlElement In XmlDoc.DocumentElement
            If xElement.Name = "appSettings" Then
                ' Loop each node of appSettings Element
                ' xNode.Attributes(0).Value , Mean First Attributes of Node ,
                ' KeyName Portion
                ' xNode.Attributes(1).Value , Mean Second Attributes of Node,
                ' KeyValue Portion
                For Each xNode As XmlNode In xElement.ChildNodes
                    If xNode.Attributes(0).Value = KeyName Then
                        xNode.Attributes(1).Value = KeyValue
                    End If
                Next
            End If
        Next
        ' Save app.config file
        XmlDoc.Save(ApppPath & "\vbbterm.txt")

    End Sub

    Public Shared Function getmenuitem()


        'Dim theXMLSource As String = (ApppPath & "\menu.xml")
        'Dim XmlDoc As New XmlDocument
        Dim reader As XmlTextReader = New XmlTextReader(ApppPath & "\menu.xml")
        Dim itemname As String
        Dim udtNew As menuitemdata
        udtNew = Nothing
        itemname = ""
        'Dim keyvalue, menuitem, menuitemvalue As String
        Do While (reader.Read())
            Select Case reader.NodeType
                Case XmlNodeType.Element 'Display beginning of element.
                    Console.Write("<" + reader.Name)
                    itemname = reader.Name
                    If reader.HasAttributes Then 'If attributes exist
                        While reader.MoveToNextAttribute()
                            'Display attribute name and value.
                            udtnew.keyvalue = reader.Value
                        End While
                    End If
                    Console.WriteLine(">")
                Case XmlNodeType.Text 'Display the text in each element.

                    'MsgBox(itemname)

                    If itemname = "visible" Then
                        udtnew.visible = reader.Value
                        Console.WriteLine(reader.Value)
                    End If
                    If itemname = "text" Then
                        udtnew.keytext = reader.Value
                        Console.WriteLine(reader.Value)
                    End If
                    If itemname = "type" Then
                        udtnew.type = reader.Value
                        Console.WriteLine(reader.Value)
                    End If
                    If itemname = "url" Then
                        udtnew.url = reader.Value
                        Console.WriteLine(reader.Value)
                    End If


                Case XmlNodeType.EndElement 'Display end of element.
                    Console.Write("</" + reader.Name)
                    Console.WriteLine(">")

            End Select



        Loop
        Return udtNew
        Console.ReadLine()




    End Function

    Public Shared Function ReadAppSettings(ByVal KeyName As String)


        'Dim rowsvalue As String
        'Dim XmlReader As New XmlTextReader(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile)
        Dim XmlDoc As New XmlDocument()
        Dim keyvalue As String
        keyvalue = ""
        ' Load XML Document
        XmlDoc.Load(ApppPath & "\vbbterm.txt")
        ' Navigate Each XML Element of app.Config file
        'MsgBox(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile.ToString)
        For Each xElement As XmlElement In XmlDoc.DocumentElement
            If xElement.Name = "appSettings" Then
                ' Loop each node of appSettings Element
                ' xNode.Attributes(0).Value , Mean First Attributes of Node ,
                ' KeyName Portion
                ' xNode.Attributes(1).Value , Mean Second Attributes of Node,
                ' KeyValue Portion
                For Each xNode As XmlNode In xElement.ChildNodes
                    If xNode.Attributes(0).Value = KeyName Then
                        keyvalue = xNode.Attributes(1).Value
                    End If
                Next
            End If
        Next
        Return keyvalue


    End Function
    Public Shared Sub AddAppSettings(ByVal KeyName, ByVal value)

        'MsgBox(KeyName & " " & KeyValue)

        '  AppDomain.CurrentDomain.SetupInformation.ConfigurationFile
        ' This will get the app.config file path from Current application Domain


        Dim XmlDoc As New XmlDocument()

        ' Load XML Document

        XmlDoc.Load(ApppPath & "\vbbterm.txt")

        Dim node As XmlNode = XmlDoc.SelectSingleNode("//appSettings")



        Dim elem As XmlElement

        elem = XmlDoc.CreateElement("add")
        elem.SetAttribute("key", KeyName)
        elem.SetAttribute("value", value)
        node.AppendChild(elem)

        ' Save app.config file
        XmlDoc.Save(ApppPath & "\vbbterm.txt")

    End Sub
    Public Shared Sub DeleteAppSettings(ByVal KeyName)


        Dim XmlDoc As New XmlDocument()

        XmlDoc.Load(ApppPath & "\vbbterm.txt")

        Dim nod As XmlNode = XmlDoc.SelectSingleNode(KeyName)
        If nod IsNot Nothing Then
            ' Since we found Bob's node, we will remove
            ' it and all of his information.
            nod.ParentNode.RemoveChild(nod)
        Else
            ' Where's Bob?
            MessageBox.Show("Couldn't find Bob.", "Where's Bob?", MessageBoxButtons.OK, _
                MessageBoxIcon.Information)
        End If


        ' Save app.config file
        XmlDoc.Save(ApppPath & "\vbbterm.txt")

    End Sub
End Class

No comments: