Accessing configuration files programatically CodeSample - XML reading PrivateVersion

From Visual WebGui Wiki

Jump to: navigation, search


Overview

This codesample shows how to use generic XML reading of web.config to read whatever value you choose from that file. In this sample we read the PrivateVersion setting.

VB.NET Code

Imports System.IO
Imports System.Xml
 
Public Function GetPrivateVersion(ByVal webConfigPath As String) As Integer
    Dim ret As Integer = 0
 
    'Retrieve the physical path of the web.config file.
    Dim configXmlDoc As New XmlDocument()
    configXmlDoc.Load(webConfigPath)
 
    ' read the config settings
    Dim oList As XmlNodeList = configXmlDoc.SelectNodes("configuration/WebGui/PrivateVersion")
    For i As Integer = 0 To oList.Count - 1
        If oList(i).Name = "PrivateVersion" Then
            Dim oValue As XmlAttribute = oList(i).Attributes("Value")
            ret = CInt(oValue.Value)
        End If
    Next
    configXmlDoc = Nothing
    Return ret
End Function

C# Code

Personal tools