2

Little bit of background first on my goal then I will show you my code and which approach I currently view as being the best approach at this moment in time.

My Goal : I have an XML file where I have 5 values stored which I want to create a byte array with these values

I have the following command hardcoded in my application using Microsoft.PointOfService :

m_Printer.PrintNormal(PrinterStation.Receipt, System.Text.ASCIIEncoding.ASCII.GetString(new byte[] { 27, 112, 48, 55, 121 }))

As you can see, the byte array is hardcoded.

This code works ok, but what I have done is created an XML file which will be read to read in the values ( these values are used to fire a cash drawer connected via a RJ11 cable to an EPSON printer ) so that the user can change the xml and not have to recompile the code.

The XML is below :

<OpenCashDrawerCommand> 
    <Byte1 value="27"/> 
    <Byte2 value="112"/> 
    <Byte3 value="48"/> 
    <Byte4 value="55"/> 
    <Byte5 value="121"/> 
</OpenCashDrawerCommand>

So you can see I am taking the hardcoded values from the C# code and reading it into 5 variables somewhere in my code.

I then want to create a byte array using these 5 values and pass the new byte array like this:

m_Printer.PrintNormal(PrinterStation.Receipt, System.Text.ASCIIEncoding.ASCII.GetString(MYNEWBYTEARRAY);

This link contains an approach which I have considered using : Converting a list of ints to a byte array

var bytes = integers.Select(i => BitConverter.GetBytes(i)).ToArray();

So, if i can take the values in my xml ( which i am already reading into a class currently ) and build a byte array, i can then just grab that byte array ( which can be static and more than likely will be for my singleton class ) then that will be job done :)

Any thoughts dudes and dudettes?

Thanks alot :)

3
  • Have you tried it? Does it do what you want? (It looks like it should). In general, questions such as "any thoughts" are going to be considered "off-topic": stackoverflow.com/faq#dontask Commented Feb 12, 2013 at 21:57
  • 1
    Are you set on using the "byte1", "byte2", ... naming convention? Commented Feb 12, 2013 at 22:04
  • Since you're just taking the byte array you get from the XML and doing System.Text.ASCIIEncoding.ASCII.GetString(byteArray), wouldn't it make more sense just to put the ASCII string as a field in the XML file, rather than the bytes of the characters as integers? Commented Feb 12, 2013 at 22:55

2 Answers 2

1

Well you need to extract the values from the XML. I'll assume you can use XLINQ:

XDocument doc = XDocument.Parse(xmlString);
XElement root = from e in doc.DocumentElement/*don't remember the exact name*/;
var byteElements = new [] { root.Element("Byte1"), root.Element("Byte2"), ... };
var bytes =
 byteElements
 .Select(elem => (byte)elem); //this "cast" is an implicit conversion operator
 .ToArray();

And there you go. If I got something minor wrong you'll be able to fix it.

Sign up to request clarification or add additional context in comments.

Comments

1

I would say scrap the "ByteN" naming convention and just use XML serialization to turn into/from xml:

[Serializable]
public class SomeClass
{
    // Serialize the list as an array with the form:
    //  <OpenDrawerCommand>
    //      <byte>...</byte>
    //      <byte>...</byte>
    //      <byte>...</byte>
    //  </OpenDrawerCommand>
    [System.Xml.Serialization.XmlArray("OpenDrawerCommand")]
    [System.Xml.Serialization.XmlArrayItemAttribute("byte")]
    public List<byte> CommandBytes {get; set;}
}


void Main()
{
    var cmd = new SomeClass() 
    { 
        CommandBytes = new List<byte> { 27, 112, 48, 55, 121 }
    };
    var originalBytes = cmd.CommandBytes;

    var sb = new StringBuilder();
    var ser = new System.Xml.Serialization.XmlSerializer(typeof(SomeClass));
    using(var sw = new StringWriter(sb))
    using(var xw = XmlWriter.Create(sw))
        ser.Serialize(xw, cmd);
    Console.WriteLine(sb.ToString());

    cmd = new SomeClass();
    Debug.Assert(cmd.CommandBytes == null);

    using(var sr = new StringReader(sb.ToString()))
    using(var xr = XmlReader.Create(sr))
        cmd = (SomeClass)ser.Deserialize(xr);
    Debug.Assert(cmd.CommandBytes.SequenceEqual(originalBytes));

    Console.WriteLine(string.Join(", ", cmd.CommandBytes));
}

The XML from the above looks like:

<?xml version="1.0" encoding="utf-16"?>
<SomeClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <OpenDrawerCommand>
        <byte>27</byte>
        <byte>112</byte>
        <byte>48</byte>
        <byte>55</byte>
        <byte>121</byte>
    </OpenDrawerCommand>
</SomeClass>

1 Comment

Thanks for your time JerKimball for looking into this question .. much appreciated :)

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.