Reading \r From File As Carriage Return

D

Dinsdale

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
 
D

Dinsdale

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
 
M

MIke Brown

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.
 
D

Dinsdale

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

Russ
 

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