示例#1
0
        private FileWithAttributes Processing(object file)
        {
            var data = file as FileWithAttributes;

            if (data == null && file is FileInfo)
            {
                data = new FileWithAttributes(file as FileInfo);
            }

            if (data != null)
            {
                switch (Command.ToUpper())
                {
                case "XPATH":
                    var xmlDoc = data.GetXml();

                    var ns = (XmlNamespaceManager)null;

                    if (!String.IsNullOrEmpty(NS))
                    {
                        var arr = NS.Split(':');
                        ns = new XmlNamespaceManager(xmlDoc.NameTable);
                        ns.AddNamespace(arr[0], arr[arr.Length - 1]);
                    }

                    for (var i = 0; i < XPATH.Length; i++)
                    {
                        var xpath = XPATH[i];
                        var name  = GetAttributeName(i);

                        var nodes = ns == null?
                                    xmlDoc.DocumentElement.SelectNodes(xpath) :
                                        xmlDoc.DocumentElement.SelectNodes(xpath, ns);

                        if (nodes == null || nodes.Count == 0)
                        {
                            data.AddAttribute(name, "");
                        }
                        else
                        {
                            var xml = new List <string>();
                            foreach (XmlNode item in nodes)
                            {
                                xml.Add(xmlResult ? item.InnerXml : item.InnerText);
                            }

                            data.AddAttribute(name, string.Join(",", xml.ToArray()) ?? "");
                        }
                    }

                    break;

                case "CRC16":
                    byte high, low;
                    var  fileBody = data.GetBytes();
                    CRC16(fileBody, fileBody.Length, out high, out low);
                    data.AddAttribute("CRC16", $"{high.ToString("x2")}{low.ToString("x2")}");
                    break;
                }
            }

            return(data);
        }