zoukankan      html  css  js  c++  java
  • [C#]XmlDocument_修改xml文件操作.

    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();
            }
        }
    }
  • 相关阅读:
    mysql创建账号
    CentOS 6.4下Squid代理服务器的安装与配置
    匿名函数 闭包
    array_values打破原有的数组key值,重新从0往上排序
    鹏哥yh搜索
    mosh的安装和使用
    提高php效率的技巧
    svn服务器配置
    限制input输入两位小数
    小程序弹出层覆盖不了canvas
  • 原文地址:https://www.cnblogs.com/fcar/p/2643391.html
Copyright © 2011-2022 走看看