C# 使用单个值/变量构建字节数组[4]

C# 使用单个值/变量构建字节数组[4],c#,.net,C#,.net,首先介绍一下我的目标,然后我将向您展示我的代码,以及目前我认为最好的方法 我的目标是:我有一个XML文件,其中存储了5个值,我想用这些值创建一个字节数组 我在应用程序中使用Microsoft.PointOfService硬编码了以下命令: m_Printer.PrintNormal(PrinterStation.Receipt, System.Text.ASCIIEncoding.ASCII.GetString(new byte[] { 27, 112, 48, 55, 121 })) 如您所

首先介绍一下我的目标,然后我将向您展示我的代码,以及目前我认为最好的方法

我的目标是:我有一个XML文件,其中存储了5个值,我想用这些值创建一个字节数组

我在应用程序中使用Microsoft.PointOfService硬编码了以下命令:

m_Printer.PrintNormal(PrinterStation.Receipt, System.Text.ASCIIEncoding.ASCII.GetString(new byte[] { 27, 112, 48, 55, 121 }))
如您所见,字节数组是硬编码的

这段代码工作正常,但我已经创建了一个XML文件,该文件将被读取以读取值(这些值用于启动通过RJ11电缆连接到爱普生打印机的现金抽屉),这样用户就可以更改XML,而不必重新编译代码

XML如下所示:

<OpenCashDrawerCommand> 
    <Byte1 value="27"/> 
    <Byte2 value="112"/> 
    <Byte3 value="48"/> 
    <Byte4 value="55"/> 
    <Byte5 value="121"/> 
</OpenCashDrawerCommand>
此链接包含我考虑使用的方法:

因此,如果我可以获取xml中的值(我目前已经在将其读入一个类中)并构建一个字节数组,那么我就可以获取该字节数组(该数组可以是静态的,很可能是针对我的singleton类的),然后这项工作就完成了:)

有什么想法吗,伙计们和姑娘们


非常感谢:)

您需要从XML中提取值。我假设您可以使用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();
就这样。如果我出了点小毛病,你就可以解决。

我会说,放弃“ByteN”命名约定,只需使用XML序列化将其转换为XML或从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));
}
[可序列化]
公共类
{
//使用以下格式将列表序列化为数组:
//  
//      ...
//      ...
//      ...
//  
[System.Xml.Serialization.XmlArray(“OpenDrawerCommand”)]
[System.Xml.Serialization.XmlArrayItemAttribute(“字节”)]
公共列表CommandBytes{get;set;}
}
void Main()
{
var cmd=new SomeClass()
{ 
CommandBytes=新列表{27、112、48、55、121}
};
var originalBytes=cmd.CommandBytes;
var sb=新的StringBuilder();
var ser=new System.Xml.Serialization.XmlSerializer(typeof(SomeClass));
使用(var sw=新的StringWriter(sb))
使用(var xw=XmlWriter.Create(sw))
序列序列化(xw,cmd);
Console.WriteLine(sb.ToString());
cmd=newsomeclass();
Assert(cmd.CommandBytes==null);
使用(var sr=newstringreader(sb.ToString()))
使用(var xr=XmlReader.Create(sr))
cmd=(SomeClass)序列反序列化(xr);
Assert(cmd.CommandBytes.SequenceEqual(originalBytes));
WriteLine(string.Join(“,”,cmd.CommandBytes));
}
上面的XML看起来像:

<?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>

27
112
48
55
121

您试过了吗?它能满足你的需要吗?(看起来应该如此)。一般来说,“任何想法”之类的问题都会被视为“离题”:您是否打算使用“字节1”、“字节2”。。。命名约定?由于您只是从XML中获取字节数组并执行
System.Text.ascienceoding.ASCII.GetString(byteArray)
,将ASCII字符串作为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));
}
<?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>