Jerry,let Tom go!

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
 
using System.Windows.Forms;
using System.Xml;
using System.IO;
using System.Text.RegularExpressions;
 
namespace xml_dataset
{
    public partial class OSCAPP : Form
    {
        public OSCAPP()
        {
            InitializeComponent();
        } 
        private void Form1_DragDrop(object sender, DragEventArgs e)
        {
            string path = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
            listBox1.Items.Add(path);//显示文件夹目录
 
        }
 
        private void Form1_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
                e.Effect = DragDropEffects.Link;
            else
                e.Effect = DragDropEffects.None;
        }
 
 
        private void button1_Click(object sender, EventArgs e)
        {
 
            if (listBox1.Items.Count == 0)
            {
                MessageBox.Show("no file name ");
            }
            else
            {
                func_SearchFiles(sender, e);//取得文件名
            }
        }
 
        public int getHex(string src)
        {
            int hex, hex2;
            string [] buf = new string [2];
            buf[0] = src.Substring(0,1);
            buf[1] = src.Substring(1,1);
            
            switch (buf[0])
            {
                case "1": hex = 0x10; break;
                case "2": hex = 0x20; break;
                case "3": hex = 0x30; break;
                case "4": hex = 0x40; break;
                case "5": hex = 0x50; break;
                case "6": hex = 0x60; break;
                case "7": hex = 0x70; break;
                case "8": hex = 0x80; break;
                case "9": hex = 0x90; break;
                case "0": hex = 0x00; break;
                case "a":
                case "A": hex = 0xa0; break;
                case "b":
                case "B": hex = 0xb0; break;
                case "c":
                case "C": hex = 0xc0; break;
                case "d":
                case "D": hex = 0xd0; break;
                case "e":
                case "E": hex = 0xe0; break;
                case "f":
                case "F": hex = 0xf0; break;
                default: return 0;
            } 
            switch (buf[1])
            {
                case "1": hex2 = 0x01; break;
                case "2": hex2 = 0x02; break;
                case "3": hex2 = 0x03; break;
                case "4": hex2 = 0x04; break;
                case "5": hex2 = 0x05; break;
                case "6": hex2 = 0x06; break;
                case "7": hex2 = 0x07; break;
                case "8": hex2 = 0x08; break;
                case "9": hex2 = 0x09; break;
                case "0": hex2 = 0x00; break;
                case "a":
                case "A": hex2 = 0x0a; break;
                case "b":
                case "B": hex2 = 0x0b; break;
                case "c":
                case "C": hex2 = 0x0c; break;
                case "d":
                case "D": hex2 = 0x0d; break;
                case "e":
                case "E": hex2 = 0x0e; break;
                case "f":
                case "F": hex2 = 0x0f; break;
                default: return 0;
            }
            return hex + hex2;  
        }
 
        int func_calc(int data)
        {
            
            string [] calc = new string[2];
 
            calc[0] = textBox2.Text.Substring(0, 1);
            calc[1] = textBox2.Text.Substring(1, 1);
            int k = Convert.ToInt16(calc[1]);
            switch (calc[0])
            {
                case "-": data -= k; break;
                case "+": data += k; break;
                case "*": data *= k; break;
                case "/": data /= k; break;
                default: data = 0; break;
            }
            return data;
 
        }
 
       string func_setX(string strCMP)
        {
            Regex r = new Regex("X"); // Split on hyphens.
            string[] s = r.Split(strCMP);
            string str = s[0];
            for (int i = 1; i < s.Length;i++ )
            { 
                    string strtemp = s[i].Substring(0,2);
                    int data = getHex(strtemp);
 
                    data = func_calc(data);//响应计算
 
                    strtemp = Convert.ToString(data, 16);//sprintf(str,"%02X", data);
                    strtemp = strtemp.PadLeft(2, '0');
                    strtemp = strtemp.PadLeft(3, 'X').ToUpper();//UPCASE
 
                   // strtemp = String.Format("X{0}", data.ToString());
                    //strtemp = "X" + data.ToString();
                    strtemp += s[i].Substring(2);
                    s[i] = strtemp;
                    str += s[i];
            } 
            return str;
        }
        void func_readline(string xmlfile)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(xmlfile);
 
 
            //XmlNodeList nodes = doc.SelectNodes("*//X");
            XmlNodeList nodes = doc.SelectNodes("/AUTO/*");//xmlchild
 
            foreach (XmlNode node in nodes)//遍历所有CMP公式
            {
 
                if (node.Name.Contains(textBox1.Text))
                    continue;
                else
                {
                    string path = "//"+ node.Name+"/*[@CMP]";//当前节点的所有子节点(含有CMP属性的) 
                    XmlNodeList nodes2 = node.SelectNodes(path);
                    foreach (XmlNode node3 in nodes2)//遍历所有CMP公式
                    {
                        string strCPM = node3.Attributes["CMP"].Value;
                        strCPM = func_setX(strCPM);
                        node3.Attributes["CMP"].Value = strCPM;
                    }
                }
            }
            doc.Save(xmlfile);
        }
 
        void func_SearchFiles(object sender, EventArgs e)
        {
            // 获取指定文件夹目录 
            string filename = listBox1.Items[0].ToString();
            int num = listBox1.Items.Count;
            for (int i = 0; i < num; i++ )
            {
                string xmlfile = @filename;
                func_readline(xmlfile);
                listBox1.Items.Add("正在处理:" + @filename);
                listBox1.SelectedIndex = listBox1.Items.Count - 1;
            }
            //xmldoc.Save(@filename);
            //    mstream.Close();
            //}
            // 显示到 Label 标签上
            listBox1.Items.Add("Finish!!!!");
            listBox1.SelectedIndex = listBox1.Items.Count - 1;
        }
 
        private void button2_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
        }
    }
}
posted on 2012-08-17 08:25  老王车辆诊断解码  阅读(2773)  评论(0编辑  收藏  举报