By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,002 Members | 2,157 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,002 IT Pros & Developers. It's quick & easy.

Reading \r From File As Carriage Return

P: n/a
I have an xml file that is read into an object using serialization. One
of the objects has a string field called delimeter that I want to
contain a carriage return. Instead of trying to include the carriage
return, I used "\r" thinking that when it was read back into the object
it would be interpreted as a carriage return. Instead, I am getting the
string literal instead of the escape sequence. Does anybody have a
slick way of telling it to return a carriage return? I'd rather not
have to use special cases for escape characters, but I will if
necessary.

Cheers!
Russ

Jun 13 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
For expediency sake, I have implemented the following code:

private char[] ReturnDelimiter(string strDelimiter)
{
char[] achrDelimiter;
switch(strDelimiter)
{
case "\r":
achrDelimiter = new char[]{'\r'};
break;
case "\n":
achrDelimiter = new char[]{'\n'};
break;
case "\r\n":
achrDelimiter = new char[]{'\r','\n'};
break;
case "\t":
achrDelimiter = new char[]{'\t'};
break;
default:
achrDelimiter = strDelimiter.ToCharArray();
break;
}

return achrDelimiter;
}

But I'd still like to know if there is a better way to do this.
Especially if it can be read in as an escape sequence from the file
during serialization.

Cheers
Russ

Dinsdale wrote:
I have an xml file that is read into an object using serialization. One
of the objects has a string field called delimeter that I want to
contain a carriage return. Instead of trying to include the carriage
return, I used "\r" thinking that when it was read back into the object
it would be interpreted as a carriage return. Instead, I am getting the
string literal instead of the escape sequence. Does anybody have a
slick way of telling it to return a carriage return? I'd rather not
have to use special cases for escape characters, but I will if
necessary.

Cheers!
Russ


Jun 13 '06 #2

P: n/a
Dinsdale,

Before you de-serialize the object, couldn't you read the file into a
string, do a String.Replace() and convert the \r's to carriage returns,
then dump it into a stream to be read by the xml deserialize?

Is the file a binary object or text-readable?

<?
TextReader r = new StreamReader( "something.xml" );
string datastr=r.ReadToEnd();
datastr.Replace("\\r","\r");
StringReader str = new StringReader(datastr);
newList = (ShoppingList)s.Deserialize( str );
?>

You might want to double check that cause I did it from memory, but it
should work.
Michael Brown
360 Replays Ltd.

Jun 14 '06 #3

P: n/a
Great suggestion Mike, thanks. It never ceases to amaze me what other
people can see that I miss. :)

Russ

MIke Brown wrote:
Dinsdale,

Before you de-serialize the object, couldn't you read the file into a
string, do a String.Replace() and convert the \r's to carriage returns,
then dump it into a stream to be read by the xml deserialize?

Is the file a binary object or text-readable?

<?
TextReader r = new StreamReader( "something.xml" );
string datastr=r.ReadToEnd();
datastr.Replace("\\r","\r");
StringReader str = new StringReader(datastr);
newList = (ShoppingList)s.Deserialize( str );
?>

You might want to double check that cause I did it from memory, but it
should work.
Michael Brown
360 Replays Ltd.


Jun 14 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.