<< Click to Display Table of Contents >> Navigation: ThinBASIC Modules > AppConfig > cAppConfig Class > cAppConfig Class usage example |
cAppConfig class usage example
Imagine your XML configuration file like the following:
<?xml version="1.0" encoding="utf-8"?>
<AppConfig>
<!--this is a comment-->
<AppVersion>1.0.0</AppVersion>
<AppData>
<Author_eMail>support@thinbasic.com</Author_eMail>
<Author_WebSite>https://www.thinbasic.com</Author_WebSite>
</AppData>
<AppFiles>
<File1>NameFile1.txt</File1>
<File2>NameFile2.txt</File2>
</AppFiles>
<Buffers>
<Buffer1><![CDATA[some stuff]]></Buffer1> <!-- Comment -->
</Buffers>
<DB>
<ConnectionString>Provider=SQLNCLI11;Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;</ConnectionString>
<tblCust>tCustomers</tblCust>
<tblItems>tItems</tblItems>
</DB>
</AppConfig>
The following code will list all the nodes present in the XML configuration file and will show some of their values
uses "Console"
uses "AppConfig"
function TBMain() as long
LoadAppConfig
end function
function LoadAppConfig() as Long
dim MyAppConfig as new cAppConfig'(APP_SourcePath & "MyAppConfig.xml")
dim nKeys as Long
dim lIdx as Long
PrintL "Active ProgId object is:", MyAppConfig.ProgId
'MyAppConfig.ProgId = "Msxml2.DOMDocument"
'printl "Active activex object", MyAppConfig.ProgId
PrintL
MyAppConfig.Load(APP_SourcePath & "MyAppConfig.xml")
'MyAppConfig.LoadXML(Load_File(APP_SourcePath & "MyMyAppConfig.xml"))
if MyAppConfig.ErrorPresent Then
'---Some error occurred
PrintL "Error code", MyAppConfig.ErrorCode
PrintL "Error description", MyAppConfig.ErrorDescription
Else
'---No errors, we can go on
printl "List all nodes present into configuration file:" in %CCOLOR_FYELLOW
nKeys = MyAppConfig.GetKeyCount
printl "Number of keys found:", nKeys
for lIdx = 1 to nKeys
printl $tab, lIdx, MyAppConfig.GetKeyName(lIdx)
Next
PrintL
PrintL "Show some specific nodes data:" in %CCOLOR_FYELLOW
PrintL "AppVersion.............", MyAppConfig.GetKey("AppVersion")
PrintL "AppData\Author_eMail...", MyAppConfig.GetKey("AppData\Author_eMail")
PrintL "AppData\Author_WebSite.", MyAppConfig.GetKey("AppData\Author_WebSite")
PrintL "AppFiles\File1.........", MyAppConfig.GetKey("AppFiles\File1")
PrintL "AppFiles\File2.........", MyAppConfig.GetKey("AppFiles\File2")
PrintL "Buffers\Buffer1........", MyAppConfig.GetKey("Buffers\Buffer1")
PrintL "DB\ConnectionString....", MyAppConfig.GetKey("DB\ConnectionString")
PrintL
end if
PrintL "Press a key to end" in %CCOLOR_FLIGHTRED
WaitKey
end Function