Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use XML to config Delphi XE2 application 1

Status
Not open for further replies.

DeniseBDA

Programmer
May 28, 2012
6
BM
Hi,
I'm trying to use an XML file to determine wiether or not I'm using the test or live database. Can you give me some info on how to achieve this or where to look.
Thanks!

 
Does it have to be an XML file? I personally find it extremely easier using an INI file ("TIniFile"). Not quite a hierarchy structure like XML, but does the job quite nicely and quick to implement (much quicker and easier than XML). I'm assuming this is the only thing you will be using this configuration file for.

For INI Files, refer to the FAQ I posted here a while back: [URL unfurl="true"]http://tek-tips.com/faqs.cfm?fid=7543[/url]


JD Solutions
 
thanks,
We have been using the INI file for the past 7 years but due to auditor's request we have to do things a little different. We have multiple udl's holding the paths but were hoping to put it all in one file. Is one way better than another??
 
Well it's not necessarily a matter of is one better than the other, it's a matter of what it will be used for. Ini Files are indeed a weaker solution for storing more complex application settings - just look at the XML Config file of any large website. XML truly provides a much more enhanced way of storing your data. Writing XML is fairly easy, however reading XML is quite a chore, more than Ini. You need to really parse through XML in many different ways, whereas Ini is very straightforward and isn't that flexible - you just read line by line. So performance is also a reason. For saving one or two string values and nothing else, Ini is perfect. For a deep complicated configuration, including lists and lists within lists (hierarchy tree), XML would be the answer.


JD Solutions
 
So here's my scenario. We are removing permissions from our application and using database groups instead. The auditors don't like the fact that us programmers can access the live database. We are a small company and the users depend on us heavily. I need to use the XML file to hold the database connection and within Delphi depending on environment parameters passed in it will connect to the live for users and development for us developers. We have this currently working with UDL's but we'd rather have all the connections in one file. Any thoughts?
 
That process is fine for what you're needing. I do that with many of my programs. In my case I alternate between using an AbsoluteDB database or an AccuRacer database to hold my settings. It really is an .ini within a database. It just means that anyone can't just look at the contents without doing some leg work. XML files, .ini files and .UDL files are all non-secure if you're storing passwords so if your auditors are concerned about security then none of those options, by default, are secure.

What part are you having a challenge with?
 
The best "Secure" solution I could see in place for this situation is exactly as you described, a file which only contains connection info (INI would work fine). The INI files of the developers will contain only connection strings which they are permitted. But when their source is published, the final project's INI file will contain the production connection string, which the developers will never see. Any SQL scripts are run by a DBA who analyzes the scripts before executing them on production. More good practice is to have QA and Test servers. But again I'm going into more detail than was asked.

One INI file may look like this:

Code:
[Application Settings]
AutoLogin=0
[Connection Strings]
AppConn=Data Source=MyServer;Initial Catalog=MyDB;User ID=sa;Password=MyPassword;Provider=SQLOLEDB.1;Persist Security Info=True
UserConn=Data Source=UserServer;Initial Catalog=UserDB;User ID=user;Password=UserPassword;Provider=SQLOLEDB.1;Persist Security Info=True

and it can be read like this:

Code:
uses
  IniFiles;

var
  AutoLogin: Bool;
  AppConnStr: String;
  UserConnStr: String;

function LoadSettings(const AFilename: String);
var
  I: TIniFiles;
begin
  I:= TIniFile.Create(AFilename);
  try
    AutoLogin := I.ReadBool('Application Settings', 'AutoLogin');
    AppConStr := I.ReadString('Connection Strings', 'AppConn');
    UserConStr := I.ReadString('Connection Strings', 'UserConn');
  finally
    I.Free;
  end;
end;


JD Solutions
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top