Ejemplo n.º 1
0
        static Protocol()
        {
            ushort Company = 0;

            StructBlock   Block   = new StructBlock();
            StructReceipt Receipt = new StructReceipt();
            StructValue   Value;

            string GroupElement = "";

            XmlReader xmlReader = XmlReader.Create(Properties.Settings.Default.Protocol);

            while (xmlReader.Read())
            {
                if (xmlReader.NodeType == XmlNodeType.Whitespace)
                {
                    // пропускаем
                }
                else if ((xmlReader.NodeType == XmlNodeType.Element) && (xmlReader.Name == "Company") && xmlReader.HasAttributes)
                {
                    Company = Convert.ToUInt16(xmlReader.GetAttribute("Code"));
                }
                else if ((xmlReader.NodeType == XmlNodeType.Element) && (xmlReader.Name == "Block") && xmlReader.HasAttributes)
                {
                    Block = new StructBlock();

                    try { Block.Name = xmlReader.GetAttribute("Name").Trim(); }
                    catch { Block.Name = ""; }

                    try { Block.Table = xmlReader.GetAttribute("Table").Trim(); }
                    catch { Block.Table = ""; }

                    Block.Code = Convert.ToUInt16(xmlReader.GetAttribute("Code"));
                    Block.Size = Convert.ToUInt16(xmlReader.GetAttribute("Size"));

                    Block.Company = Company;

                    BlockList.Add(Block);

                    GroupElement = "Block";
                }
                else if ((xmlReader.NodeType == XmlNodeType.EndElement) && (xmlReader.Name == "Block"))
                {
                    GroupElement = "";
                }
                else if ((xmlReader.NodeType == XmlNodeType.Element) && (xmlReader.Name == "Receipt") && xmlReader.HasAttributes)
                {
                    Receipt = new StructReceipt();

                    try { Receipt.Name = xmlReader.GetAttribute("Name").Trim(); }
                    catch { Receipt.Name = ""; }

                    try { Receipt.Table = xmlReader.GetAttribute("Table").Trim(); }
                    catch { Receipt.Table = ""; }

                    Receipt.Code = Convert.ToUInt16(xmlReader.GetAttribute("Code"));
                    Receipt.Size = Convert.ToUInt16(xmlReader.GetAttribute("Size"));

                    Receipt.Company = Company;

                    ReceiptList.Add(Receipt);

                    GroupElement = "Receipt";
                }
                else if ((xmlReader.NodeType == XmlNodeType.EndElement) && (xmlReader.Name == "Receipt"))
                {
                    GroupElement = "";
                }
                else if ((xmlReader.NodeType == XmlNodeType.Element) && (xmlReader.Name == "Value") && xmlReader.HasAttributes)
                {
                    Value = new StructValue();

                    try { Value.Name = xmlReader.GetAttribute("Name").Trim(); }
                    catch { Value.Name = ""; }

                    try { Value.Type = xmlReader.GetAttribute("Type").Trim(); }
                    catch { Value.Type = ""; }

                    try { Value.Base = Convert.ToByte(xmlReader.GetAttribute("Base")); }
                    catch { Value.Base = 10; }

                    try { Value.Field = xmlReader.GetAttribute("Field").Trim(); }
                    catch { Value.Field = ""; }

                    try { Value.Parameter = xmlReader.GetAttribute("Parameter").Trim(); }
                    catch { Value.Parameter = ""; }

                    Value.Offset = Convert.ToUInt16(xmlReader.GetAttribute("Offset"));
                    Value.Size   = Convert.ToUInt16(xmlReader.GetAttribute("Size"));

                    Value.AddFlag(xmlReader.GetAttribute("Flag"));

                    if (GroupElement == "Block")
                    {
                        Block.ValueList.Add(Value);
                    }
                    else if (GroupElement == "Receipt")
                    {
                        Receipt.ValueList.Add(Value);
                    }
                    else
                    {
                        throw new Exception("Error in protocol file");
                    }
                }
            }
        }