Kev
Kev

Reputation: 773

Escape Sequence in String

I have an issue where if I pass a hardcoded string \x1B|200uF as a parameter the command accepts it correctly.

But, when I retrieve the same value from an XML element into a new string variable I get the following value : \\x1B|200uF

As you can see there is an extra escape sequence.

So in summary the problem :

 using (XmlReader xmlReader = XmlReader.Create(@"PosPrinter.xml"))
                {     
                    while (xmlReader.Read())
                    {
                        if (xmlReader.NodeType == XmlNodeType.Element)
                        {
                            switch (xmlReader.Name)
                            {
                                case "PosPrinter":
                                    _printer.LogicalName = xmlReader.GetAttribute("LogicalName");
                                    break;

                                case "FeedReceiptCommand":
                                    _printer.FeedReceiptCommand = xmlReader.GetAttribute("value");
                                    break;

I retrieve the value into my 'FeedReceiptCommand' string the value as I mentioned above is stored in the xml as \x1B|200uF but is retrieved into the string as \\x1B|200uF with the extra escape sequence at the beginning.

I then call my command using the string variable FeedReceiptCommand :

 _posPrinter.PrintNormal(PrinterStation.Receipt, PrinterSettings.FeedReceiptCommand );

But the command doesn't execute because of the extra escape sequence.

But if I call the same command with the value hardcoded:

 _posPrinter.PrintNormal(PrinterStation.Receipt, "\x1B|200uF");

... then the command gets executed successfully..

The value \x1B|200uF is the ESC command to send to a Epson TM-T88V printer using Microsoft.PointOfService whic the '\x' is for Hex I think and the 1B is the Hex value..

I have tried to get rid of the extra escape sequence by using 'Trim', 'Substring', and even doing a foreach loop to loop each char in the string to build a new one. I also tried stringbuilder.

But I'm missing the point somewhere here.

So any help would be appreciated in how I can pass a variable in place of the \x1B|200uF

Upvotes: 2

Views: 3038

Answers (2)

Andre Loker
Andre Loker

Reputation: 8408

The problem lies (as @OlafDietsche pointed out) indeed in the XML file. In the C# string, \x1B means "the character with code 1B (hex) or 27 (dec)", in XML it's just the four characters.

So you'll have to encode the special character inside your XML document differently. Theoretically, you'd simply replace \x1B with , which is the XML way of saying "the character number 1B (hex)". The problem in this specific case, however, is that  is not allowed in XML. The valid characters in an XML document are defined here: http://www.w3.org/TR/xml/#charsets Note how #x1B is not part of this range.

You could use a different character to represent Escape in the XML and replace it inside your C# code. Make sure to use a surrogate character that a) is a valid XML character and b) would never be used as actual data.

For example, choose xFEED as your escape char (as it is easy to recognize). So your document looks like:

<FeedReceiptCommand value="&#xFEED;|200uF"/>

In your C# code, replace it accordingly:

string actualValue = reader.GetAttribute("value").Replace('\xFEED', '\x1B')

Upvotes: 3

Olaf Dietsche
Olaf Dietsche

Reputation: 74028

In the hardcoded string, you have the character hex 1B for ESC plus the string |200uF. I haven't seen your XML, but I guess in XML, you have the string \x1B literally, which is four characters \, x, 1 and B.

That's the difference between the two, hardcoded and XML.

AFAIK, there is no way to include control characters as ESC literally in an XML 1.0 document. You might try to encode it as &#x001B; and parse it yourself, if it is not delivered properly by your XML parser.

Upvotes: 1

Related Questions