473,383 Members | 1,874 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

How do I parse name/value pairs from a config string?

If, for example, I retrieve a connectionstring from a config file using
something like:

Value = ConfigurationSettings.AppSettings["ConnectionString"];

This will return a string that is semi-colon delimited. If I want, say, to
retrieve the password from this string will I need to explicity parse it?
Jul 21 '05 #1
4 12643
Bill,
I normally use String.Split to separate each pair, then use String.Split
again to separate the pairs.

Optionally putting the name/value pairs into a HashTable or
NameValueCollection.

Instead of the second String.Split I've used String.IndexOf &
String.SubString to split the pairs, as there should be only a single split
needed...

Hope this helps
Jay

"Bill" <nf*@nospam.com> wrote in message
news:Ot*************@TK2MSFTNGP12.phx.gbl...
If, for example, I retrieve a connectionstring from a config file using
something like:

Value = ConfigurationSettings.AppSettings["ConnectionString"];

This will return a string that is semi-colon delimited. If I want, say, to
retrieve the password from this string will I need to explicity parse it?

Jul 21 '05 #2
Yep. You may use String.Split() to get individual ';' separated items and
another String.Split() (or String.StartsWith() ) on the first set to get the
password.

HTH.

"Bill" <nf*@nospam.com> wrote in message
news:Ot*************@TK2MSFTNGP12.phx.gbl...
If, for example, I retrieve a connectionstring from a config file using
something like:

Value = ConfigurationSettings.AppSettings["ConnectionString"];

This will return a string that is semi-colon delimited. If I want, say, to
retrieve the password from this string will I need to explicity parse it?

Jul 21 '05 #3
If you wanted to get really excited you could use Regular Expressions:

In VB.NET (I doubt the C# is a million miles away) :

'don't forget to import the namspace
Imports System.Text.RegularExpressions

'later...
'we're assuming a SQL Server connection string here and SQL Server
'Authentication
Dim regExp as new Regex("server=\w*;")
Dim m Match

m = regExp.Match(strDBConnectionString, "pwd=\w*;")

If m.Success Then
'you've found it...
End If

Notice the "\w*" bit is searching for a "word" in the phrase searched.

This would work with the other bits of the connection string too by tweaking
the expression BUT I have found the "word" gets confused if your server is
being identified by an IP address instead of it's name...

Probably all those "."'s :)

al*****@yahoo.com

"Bill" wrote:
If, for example, I retrieve a connectionstring from a config file using
something like:

Value = ConfigurationSettings.AppSettings["ConnectionString"];

This will return a string that is semi-colon delimited. If I want, say, to
retrieve the password from this string will I need to explicity parse it?

Jul 21 '05 #4
Thanks... Reqular Expressions strikes me as the way to go.

"alaspin" <al*****@discussions.microsoft.com> wrote in message
news:48**********************************@microsof t.com...
If you wanted to get really excited you could use Regular Expressions:

In VB.NET (I doubt the C# is a million miles away) :

'don't forget to import the namspace
Imports System.Text.RegularExpressions

'later...
'we're assuming a SQL Server connection string here and SQL Server
'Authentication
Dim regExp as new Regex("server=\w*;")
Dim m Match

m = regExp.Match(strDBConnectionString, "pwd=\w*;")

If m.Success Then
'you've found it...
End If

Notice the "\w*" bit is searching for a "word" in the phrase searched.

This would work with the other bits of the connection string too by tweaking the expression BUT I have found the "word" gets confused if your server is
being identified by an IP address instead of it's name...

Probably all those "."'s :)

al*****@yahoo.com

"Bill" wrote:
If, for example, I retrieve a connectionstring from a config file using
something like:

Value = ConfigurationSettings.AppSettings["ConnectionString"];

This will return a string that is semi-colon delimited. If I want, say, to retrieve the password from this string will I need to explicity parse it?

Jul 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: brettr | last post by:
When I reference document.cookie, there is a long string of key=value; pairs listed. I may have 100 hundred cookies on my hard drive. However, most only have one key=value pair. Does the...
5
by: Robert Oschler | last post by:
I am converting a Perl script over to "C" for a potential open source project. I need some open source "C" code that will give me the same functionality of a Perl Style associative array: ...
21
by: William Stacey [MVP] | last post by:
Anyone know of some library that will parse files like following: options { directory "/etc"; allow-query { any; }; // This is the default recursion no; listen-on { 192.168.0.225;...
1
by: Bill | last post by:
I have strings returned from a config file that contain zero or more name/value pairs using a pattern like "name = value;" I want to extract the value of a given name (passwords, etc.). I want...
4
by: Bill | last post by:
If, for example, I retrieve a connectionstring from a config file using something like: Value = ConfigurationSettings.AppSettings; This will return a string that is semi-colon delimited. If I...
5
by: Navid Azimi | last post by:
What's the best way to parse a currency string to a decimal given the possibility of multiple currencies? That is, my numeric string can be ($12.00) or -£12.00; in either case I want -12.00 to be...
1
by: ozzii | last post by:
Hi, Does anybody know how to parse the name value pairs in a querystring contained in a variable with asp? for those of you who might be confused, what i am trying to do is basically I have a...
5
by: simononestop | last post by:
Hi im totally new to perl this is my first go at using it (I normally use asp). I have set up a form with a cgi script from demon hosting. I have edited the script and the form works it sends me an...
7
by: Cirene | last post by:
I have a long string (MyString) with several token/value combinations. Here's an example... NAME: JOHN ADDRESS: 123 MAIN ST COUNTY: SOMEWHERE PHONE: 333-222-5151 x542 ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.