Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# How to read \r\n string

First of all, hi!, this is my first post!

I'm writing an application to read and write to serial ports, all is working nice, but since in my company we have different types of scanners, they are configured with different prefix/suffix characters, most of them have the \r\n sequence.

So I decided to include this suffix on my xml config file, like so:

<rPortSuffix>\r\n</rPortSuffix>

When I read my xml file, I use this

case "rPortSuffix":
    srPortSuffix = xmlReader.Value;
break;

The problem is, when the text value is passed to srPortSuffix, instead of having "\r\n", the variable stores "\\r\\n", so when I try to read the serial port, nothing happens due to the incorrect suffix string (for the NewLine constant)

spRead.NewLine = srPortSuffix; //NewLine constant read in XML file
sReadData = spRead.ReadLine(); //Storing the read data 

So I tried:

srPortSuffix= Regex.Replace(srPortSuffix, @"\\", @"\"); 

But with no success. Any tips?

TL;DR

How can I make "\r\n" value read from an xml file be read exactly like that, instead of "\\r\\n"?

Edit:

As suggested by Jirka Hanika, I simply changed my string from "\r\n" to the ascii codes, as such:

<rPortSuffix>&#13;&#10;</rPortSuffix>

And now my application is working for the 2 types of scanner I am testing it with.

Thanks!!

like image 650
desto Avatar asked Jun 29 '26 01:06

desto


2 Answers

You may be confusing actual string data with how they are represented in C# source code. Of course, the C# syntax does not apply to the XML file, so \r\n is in no way related to a newline sequence.

It's not converted to \\r\\n by the code you show, but it is not converted to a newline sequence either.

The simplest way of doing the latter is:

srPortSuffix = srPortSuffix.Replace(@"\r", "\r").Replace(@"\n", "\n");

You would basically do something that a C# compiler normally does for you, but completely outside of any C# source.

There's also the even easier alternative of not converting anything, and simply putting the newline sequence to the XML file directly:

<rPortSuffix>
</rPortSuffix>

This may however be misunderstood by some readers and/or break your pretty printing conventions (the end tag will not be indented). A dirtier but maybe more practical way of writing the same thing in XML:

<rPortSuffix>&#13;&#10;</rPortSuffix>
like image 57
Jirka Hanika Avatar answered Jul 01 '26 14:07

Jirka Hanika


I think there is a little misconception in your approach. \r\n is a "symbol", so to speak, that indicates a carriage return and a new line, in sequence. This "symbol" (or group of) are only valid to a compiler -- they mean nothing to anything else, even to runtime code.

What the compiler does is convert those to their binary representation, in compile time. So, when you do

var str = "\r\n";

The compiler actually stores something like:

{ 0xD, 0xA } 

(not implying here that a string is an array... I have no idea how strings are stored in .Net, nor should you)

So, the compiler is interpreting and transforming your 'code' into something else. My opinion here is that, since you are taking the compiler out of the equation (by reading the value in run-time) you have to do its job.

So, following this rationale, I would read the contents of the config file and then "interpret" it:

if (srPortSuffix == "\\\\r\\\\n")
{
    actualPortSuffix = "\r\n";
}

Of course, since this is all representative, what I believe would be an even better approach would be to use some other expression to represent the new line. Your config could read:

<rPortSuffix>CarriageReturn-NewLine</rPortSuffix>

Which is much more clear. In code, you'd do:

if (srPortSuffix == "CarriageReturn-NewLine")
{
    actualPortSuffix = "\r\n";
}
like image 31
Bruno Brant Avatar answered Jul 01 '26 13:07

Bruno Brant