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
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 thatis not allowed in XML. The valid characters in an XML document are defined here: http://www.w3.org/TR/xml/#charsetsNote 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:
In your C# code, replace it accordingly: