Rijswijker

Read application.exe.config (XML)

2 posts in this topic

#1 ·  Posted (edited)

Hi,

I am struggling with reading a configuration file.
What i try to achieve is an array with two columns:

  • Column 0: Name of the value for ex. UserName
  • Column 1: the value <value></value> : MyUsername

The application.exe.config file

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
      <section name="IbUpload.Agent.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
  </configSections>
  <startup>
    <supportedRuntime version="v4.0"/>
    <supportedRuntime version="v2.0.50727"/>
  </startup>
  <applicationSettings>
    <IbUpload.Settings>
      <setting name="WebServerUrl" serializeAs="String">
        <value>https://www.domain.com/xss/discovery/webservice</value>
      </setting>
      <setting name="DiarFileName" serializeAs="String">
        <value>%FacilityNo% - %CaptureDateTime%</value>
      </setting>
      <setting name="UserName" serializeAs="String">
        <value>Username</value>
      </setting>
      <setting name="Password" serializeAs="String">
        <value>Password</value>
      </setting>
      <setting name="ClientName" serializeAs="String">
        <value>APT.%FacilityNo%</value>
      </setting>
      <setting name="UpdateUrl" serializeAs="String">
        <value>https://updateserver.domain.com/release</value>
      </setting>
    </IbUpload.Settings>
  </applicationSettings>
</configuration>

I have a working solution but i want to extend the flexibility of the function.
Now i have to know all the setting names,

; Read value
Local $sAppCfgUserName = _ApplicationConfig("Application.exe.config", "UserName")
Local $sAppCfgPassword = _ApplicationConfig("Application.exe.config", "Password")

; Write value
_ApplicationConfig("Application.exe.config", "UserName", "NewUsername")
_ApplicationConfig("Application.exe.config", "Password", "NewPassword")

#Region Read or Write to Application.Config.exe
Func _ApplicationConfig($sFile, $sXmlKeyName, $sXmlValue = Null)
    Local $aFile, $rReturn = False

    ; Check if configuration file exists, if not set @error to 1
    If Not FileExists($sFile) Then
        SetError(1) ; File does not exist
        Return False
    EndIf

    ; Read config
    If Not($sXmlValue) Then
        Local $oXML = ObjCreate("Microsoft.XMLDOM")
        If IsObj($oXML) Then
            $oXML.load($sFile)
            $oParameters = $oXML.SelectNodes("//IbUpload.Settings/setting[@name='" & $sXmlKeyName & "']")

            For $oParameter In $oParameters
                $oValue = $oParameter.SelectSingleNode("./value")
                Return String($oValue.text) ; Return value
            Next
            Else
            MsgBox(0,0,2)
            SetError(2, @error) ; Error creating object
            Return ""
        EndIf
    Else
        ; Write Config
        $sXmlSearchValue = _ApplicationConfig($sFile, $sXmlKeyName)
        _FileReadToArray($sFile, $aFile)
        If @error Then
            MsgBox(0,0,3)
            SetError(3, @error)
            Return False
        EndIf

        For $a = 1 To $aFile[0]
            If StringInStr($aFile[$a], '<value>' & $sXmlSearchValue & '</value>') Then
                $sText = StringReplace(FileReadLine($sFile, $a), $sXmlSearchValue, $sXmlValue)
                _FileWriteToLine($sFile, $a, $sText, 1)
                If Not @error Then Return True
            EndIf
        Next
        Return $rReturn
    EndIf

EndFunc   ;==> _ApplicationConfig
#EndRegion

In the end i want to compare two configuration files, now i have to know all "ParentNode" names and i am not able to check if there more Nodes in one of the two configuration files.

For example, one configuration file has the setting UpdateUrl2, i want able to detect that change.

      <setting name="UpdateUrl2" serializeAs="String">
        <value>https://updateserver.domain.com/pub/software/release</value>
      </setting>

 

Edited by Rijswijker

Share this post


Link to post
Share on other sites



nobody? :blink:

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now