End of Line Marker

K

KNE

I'm writing some code that is formating an output file being generated
within a web app. I want to allow the site administrator to configure the
characters that will mark the end of each line/record in the file.
Therefore, I added a key to my web.config appSettings section similar to the
following:

<add key="EORMarker" value="\r\n" />

This way, the setting can be changed to use "\r", "\n", "\n\r", "\r\n\n",
etc. I retrieve the appSetting value with code in the Global.asax and load
it into the application cache for quick access as in:

string eorMarker =
((NameValueCollection)ConfigurationSettings.GetConfig("appSettings"))["EORMa
rker"];

Context.Cache.Insert("EORMarker", eorMarker, new
CacheDependency(Server.MapPath("web.config")));

I have a static lookup method I use to pull the string out of the cache when
needed:

public static string EORMarker()

{

return (string)HttpContext.Current.Cache["EORMarker"];

}

When I write a record/line to the output file, I tack on the characters
intended to serve as the End Of Record marker:

myStream.Write(this.myHeader + Lookup.EORMarker());

However, instead of getting an expected carriage return and line feed at the
end of the record/line, I literally get the "\r\n" characters in the output.
If I put the literal string in place of the lookup method:

myStream.Write(this.myHeader + "\r\n");

I get the desired / correct effect (a CR/LF to a new line). What can I do
to make pulling the End of Record Marker from the appSettings give me the
effect I want?

Thanks,

Ken
 
B

Bret Mulvey

The symbols \r and \n don't have any special meaning in XML like they do in
a C# string literal. To indicate these characters in XML you would use
and
 
D

Daniel O'Connell

Bret Mulvey said:
The symbols \r and \n don't have any special meaning in XML like they do in
a C# string literal. To indicate these characters in XML you would use
and
or you could simply modify EORMarker(why is that a method anyway?) to parse
and create acorrect string and store it locally, that allows end users a
much easier time writing different EOR markers, remembering the codes for
those would suck.

something like this would do it, but this is certainly not thread safe and
you'll have to do what you need to handle that(I recommend Jon Skeet's
article on the subject[1] for handling thread saftey and lazy init):

static string eorMarker=null;
public static string EORMarker {
get {
if (_eorMarker != null) {
eorMarker =
((NameValueCollection)ConfigurationSettings.GetConfig("appSettings"))["EORMa
rker"];
eorMarker = eorMarker.Replace(@"\n", "\n");
eorMarker = eorMarker.Replace(@"\r","\r");

}
return eorMarker;
}
}

[1] http://www.pobox.com/~skeet/csharp/singleton.html
KNE said:
I'm writing some code that is formating an output file being generated
within a web app. I want to allow the site administrator to configure the
characters that will mark the end of each line/record in the file.
Therefore, I added a key to my web.config appSettings section similar to the
following:

<add key="EORMarker" value="\r\n" />

This way, the setting can be changed to use "\r", "\n", "\n\r", "\r\n\n",
etc. I retrieve the appSetting value with code in the Global.asax and load
it into the application cache for quick access as in:

string eorMarker =
((NameValueCollection)ConfigurationSettings.GetConfig("appSettings"))["EORMa
rker"];

Context.Cache.Insert("EORMarker", eorMarker, new
CacheDependency(Server.MapPath("web.config")));

I have a static lookup method I use to pull the string out of the cache when
needed:

public static string EORMarker()

{

return (string)HttpContext.Current.Cache["EORMarker"];

}

When I write a record/line to the output file, I tack on the characters
intended to serve as the End Of Record marker:

myStream.Write(this.myHeader + Lookup.EORMarker());

However, instead of getting an expected carriage return and line feed at the
end of the record/line, I literally get the "\r\n" characters in the output.
If I put the literal string in place of the lookup method:

myStream.Write(this.myHeader + "\r\n");

I get the desired / correct effect (a CR/LF to a new line). What can I do
to make pulling the End of Record Marker from the appSettings give me the
effect I want?

Thanks,

Ken
 
D

Daniel O'Connell

Of course, I missed that you're using asp.net, in that case the parse code
is all you need then throw the new string into your cache, instead of
worrying about threadsafe singleton initialization.

Daniel O'Connell said:
Bret Mulvey said:
The symbols \r and \n don't have any special meaning in XML like they do in
a C# string literal. To indicate these characters in XML you would use
and
or you could simply modify EORMarker(why is that a method anyway?) to parse
and create acorrect string and store it locally, that allows end users a
much easier time writing different EOR markers, remembering the codes for
those would suck.

something like this would do it, but this is certainly not thread safe and
you'll have to do what you need to handle that(I recommend Jon Skeet's
article on the subject[1] for handling thread saftey and lazy init):

static string eorMarker=null;
public static string EORMarker {
get {
if (_eorMarker != null) {
eorMarker =
((NameValueCollection)ConfigurationSettings.GetConfig("appSettings"))["EORMa
rker"];
eorMarker = eorMarker.Replace(@"\n", "\n");
eorMarker = eorMarker.Replace(@"\r","\r");

}
return eorMarker;
}
}

[1] http://www.pobox.com/~skeet/csharp/singleton.html
((NameValueCollection)ConfigurationSettings.GetConfig("appSettings"))["EORMa
rker"];

Context.Cache.Insert("EORMarker", eorMarker, new
CacheDependency(Server.MapPath("web.config")));

I have a static lookup method I use to pull the string out of the
cache
when
needed:

public static string EORMarker()

{

return (string)HttpContext.Current.Cache["EORMarker"];

}

When I write a record/line to the output file, I tack on the characters
intended to serve as the End Of Record marker:

myStream.Write(this.myHeader + Lookup.EORMarker());

However, instead of getting an expected carriage return and line feed
at
the
end of the record/line, I literally get the "\r\n" characters in the output.
If I put the literal string in place of the lookup method:

myStream.Write(this.myHeader + "\r\n");

I get the desired / correct effect (a CR/LF to a new line). What can
I
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top