如何从整个扫描数据中仅打印数据标签?

How to print only datalabel out of whole scandata?

我是 C# 编程的新手。我有一个斑马条码扫描仪。我根据软件用户手册制作了一个C#代码。我能够打印扫描数据。但是,此数据包含有关扫描仪的所有信息。它有序列号、模块号、GUID、数据类型和数据标签。

这个数据标签是关于条码的信息。我只对这个数据标签感兴趣。我需要使用此数据标签开发更多代码。

如何只打印数据标签?

这里附上我的代码。谢谢。

namespace ConsoleApp_scandata
{
  class Program
  {
    //declare the core scanner class 
    static CCoreScannerClass cCoreScannerClass;
    static void Main(string[] args)
    {

        cCoreScannerClass = new CoreScanner.CCoreScannerClass();

        //CALL OPEN API
        short[] scannerTypes = new short[1];        //scanner types intrested in
        scannerTypes[0] = 1;                        // set all scanner types to 1
        short numberOfScannerTypes = 1;             // size of the scanner type array 
        int status;                                 // Extend API return code

        cCoreScannerClass.Open(0, scannerTypes, numberOfScannerTypes, out status);

        if (status == 0)
            Console.WriteLine("CoreScanner API OPEN");
        else
            Console.WriteLine("CoreScanner API CLOSED");

        // Lists all scanners connected to the host computer.
        // will return nothing

        short numberOfScanners;
        int[] connectedScannerIDList = new int[255];

        string outXML;

        cCoreScannerClass.GetScanners(out numberOfScanners, connectedScannerIDList, out outXML, out status);

        //below does not work because string is an xml file and is never NULL
        Console.WriteLine(outXML);
        // Console.WriteLine(outXML.ToString()); 
        cCoreScannerClass.BarcodeEvent += new _ICoreScannerEvents_BarcodeEventEventHandler(OnBarcodeEvent);
        int opcode = 1001;
        string inXML = "<inArgs>" +
            "<cmdArgs>" +
            "<arg-int>1</arg-int>" +
            "<arg-int>1</arg-int>" +
            "</cmdArgs>" +
            "</inArgs>";
        cCoreScannerClass.ExecCommand(opcode, ref inXML, out outXML, out status);

        opcode = 2011;
        inXML = "<inArgs>" +
           "<scannerID>1</scannerID>" +
              "</inArgs>";
        cCoreScannerClass.ExecCommand(opcode, ref inXML, out outXML, out status);
       

        while (true)
        {
            Console.Read();
        }
        
    }

    private static void OnBarcodeEvent(short eventType, ref string pscanData)
    {
        Console.WriteLine("Scannner Event! Scan Data: " + pscanData);
    }
  }
}

XmlDocument 中加载 pscanData 就可以开始了,只需复制此代码:


private static void OnBarcodeEvent(short eventType, ref string pscanData)
{
    Console.WriteLine("Scannner Event! Scan Data: " + pscanData);

    XmlDocument xmlDoc = new XmlDocument();

    //xmlDoc.LoadXml(pscanData); //You should use this line as far as your XML tags are correct
    xmlDoc.LoadXml("<scandata>" +

                   "<modeldata>" +
                   "099909" +
                   "</modeldata>" +

                   "<datalabel>" +
                   "0x68 0x74 0x74" +
                   "</datalabel>" +

                   "</scandata>"); //I use this harcoded XML because I don't have your pscanData, just delete this line and uncoment the one above

    XmlNodeList datalabel = xmlDoc.GetElementsByTagName("datalabel");
    XmlNode allInDataLabel = datalabel.Item(0);
    string whatDatalabelContains = allInDataLabel.InnerText;

    Console.WriteLine("Datalabel: " + ToText(whatDatalabelContains));
}

编辑:十六进制到文本


private static string ToText(string yourHex)
{
    StringBuilder sb = new StringBuilder();
    string[] dataArr = yourHex.Split(new char[] { ' ' });

    for (int i = 0; i < dataArr.Length; i++)
    {
        sb.Append(Char.ConvertFromUtf32(Convert.ToInt32(dataArr[i], 16)));
    }

    return sb.ToString();
}