2

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

1
  • 1
    What do you get if you print the value out? I'm sure that the `\` is just a display thing. Commented Feb 13, 2013 at 23:04

2 Answers 2

3

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')
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Andre ( & @OlafDietsche ) for your input and time taken to look at my issue. Marked answer by Andre as the method worked great. +1 Thanks again:)
1

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.

1 Comment

thanks Olaf for taking the time to add your input .. much appreciated :) +1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.