c# xml 参数读取的复杂使用

完整使用2

生产厂家里面包含很多规格型号,一个规格型号里面包含很多出厂序列号,点击下一步如果检测到填充的和保存的不一样 就新增一条(如检测到生产厂家相同,但是规格型号不同,就新增一组规格型号)。
界面一:新增界面
在这里插入图片描述
界面2 删除界面
在这里插入图片描述

界面一:新增界面

load

其中 因为直接使用了 节点名字作为保存,而节点名字是不能出现数字字符开头的,因此统一在各节点之前加“一”,显示的时候再给他去掉

        private void DeviceEntering_Load(object sender, EventArgs e)
        {
    
     
        //刷新
            intRefresh();
        }
        public static  List<string> changjia = new List<string>();
        public static  List<string> xinghao  = new List<string>();
        public static  List<string> xvlie    = new List<string>();
        public static List<string> testname = new List<string>();
        public static List<string> reportname = new List<string>();
        static string path = Directory.GetCurrentDirectory() + "\\Config\\测试参数.xml";
        public static string pathxml = @"C:\SEET\test.xml";
        public static string pathname = @"C:\SEET\testName.xml";
        public static string pathreport = @"C:\SEET\testReport.xml";
        public static string pathmulu = @"C:\SEET\";
      public static void intRefresh ()
        {
    
    
            //该方法是判断该路径下有没有该文件夹,注意并不能判断某个文件是否存在
            if (!Directory.Exists(pathmulu))
            {
    
    
                DirectoryInfo directoryInfo = new DirectoryInfo(pathmulu);
                directoryInfo.Create();
            }
            //该方法是判断该路径下有没有该文件,注意一定要加上文件后缀名
            if (!File.Exists(pathxml))
            {
    
    
                CreateXml();
            }
            if (!File.Exists(pathname))
            {
    
    
                XmlDocument Doc = new XmlDocument();
                XmlDeclaration xnode = Doc.CreateXmlDeclaration("1.0", "UTF-8", "");
                Doc.AppendChild(xnode);

                XmlElement root = Doc.CreateElement("测试人员");
                Doc.AppendChild(root);

                XmlElement TestName = Doc.CreateElement("一测试员");
                root.AppendChild(TestName);
                TestName.InnerText = "张三";

                Doc.Save(pathname);
            }

            if (!File.Exists(pathreport))
            {
    
    
                XmlDocument Docc = new XmlDocument();
                XmlDeclaration xnode = Docc.CreateXmlDeclaration("1.0", "UTF-8", "");
                Docc.AppendChild(xnode);

                XmlElement root = Docc.CreateElement("报告保存");
                Docc.AppendChild(root);

                XmlElement TestName = Docc.CreateElement("气腹机测试报告模板-001.docx");
                root.AppendChild(TestName);
                TestName.InnerText = "张三";

                Docc.Save(pathreport);
            }


            //实例化一个xml操作对象
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(pathxml);
            XmlNode rNode = xDoc.SelectSingleNode("气腹机");//.SelectSingleNode:取到第一个和表达式匹配的节点
            XmlNodeList rlist = rNode.ChildNodes; //获取节点的所有子节点

                                                  // //取根结点
                                                  // var root = xmlDoc.DocumentElement;//取到根结点
                                                  取指定的单个结点
                                                  //XmlNode oldChild = xmlDoc.SelectSingleNode("BookStore/NewBook");
                                                  取指定的结点的集合
                                                  //XmlNodeList nodes = xmlDoc.SelectNodes("BookStore/NewBook");


            int i = 0, j = 0, k = 0;
            changjia.Clear();
            xinghao.Clear();
            xvlie.Clear();
            foreach (XmlNode r in rlist)
            {
    
    
                string name = r.Name;//获取节点的限定名称
                name = name.Substring(1, name.Length - 1);
                //changjia.Add(name);

                changjia.Add(name);
                log.SaveLog("节点:" + name.Substring(1, name.Length - 1));
                i++;
                XmlNodeList list2 = r.ChildNodes;
                foreach (XmlNode item in list2)
                {
    
    
                    string name2 = item.Name;
                    name2 = name2.Substring(1, name2.Length - 1);
                    if (name2.Equals("text"))
                    {
    
    
                        continue;
                    }
                    j++;
                    xinghao.Add(name + "*" + name2 + "*");
                    log.SaveLog("节点2:" + name2);

                    XmlNodeList list3 = item.ChildNodes;
                    foreach (XmlNode item2 in list3)
                    {
    
    
                        k++;
                        string name3 = item2.Name;
                        name3 = name3.Substring(1, name3.Length - 1);
                        if (name3.Equals("text"))
                        {
    
    
                            continue;
                        }
                        xvlie.Add(name + "*" + name2 + "*" + name3);
                        //uiComboBox3.Items.Add(name3);
                        log.SaveLog("节点3:" + name3);
                    }
                }
            }


            string[] nameBuff = new string[500];
            Array.Clear(nameBuff, 0, nameBuff.Length);
            nameBuff = SeakName(ref xinghao, Form1.de.uiComboBox1.Text);
            Form1.de.uiComboBox2.Items.Clear();
            Form1.de.uiComboBox2.Text = GetTestPata().Type1;
            for (int jj = 0; jj < nameBuff.Length; jj++)
            {
    
    
                if (string.IsNullOrEmpty(nameBuff[jj]))
                {
    
    
                    continue;
                }
                Form1.de.uiComboBox2.Items.Add(nameBuff[jj]);
                log.SaveLog("load:uiComboBox2.Items:" + nameBuff[jj]);
            }

            Array.Clear(nameBuff, 0, nameBuff.Length);
            nameBuff = SeakName(ref xvlie, Form1.de.uiComboBox1.Text, Form1.de.uiComboBox2.Text);
            Form1.de.uiComboBox3.Items.Clear();
            Form1.de.uiComboBox3.Text = GetTestPata().Serial1;
            for (int jjj = 0; jjj < nameBuff.Length; jjj++)
            {
    
    
                if (string.IsNullOrEmpty(nameBuff[jjj]))
                {
    
    
                    continue;
                }
                Form1.de.uiComboBox3.Items.Add(nameBuff[jjj]);
                log.SaveLog("load:uiComboBox3.Items:" + nameBuff[jjj]);
            }
            xDoc.Save(pathxml);//修改好了 记得保存 才会生效

            Form1.de.uiComboBox1.Items.Clear();
            foreach (var item in changjia)
            {
    
    
                Form1.de.uiComboBox1.Items.Add(item);
            }
            Form1.de.uiComboBox1.Text = GetTestPata().Manufacturer1;

            try
            {
    
    
                testname.Clear();
                XmlDocument xx = new XmlDocument();
                xx.Load(pathname);
                XmlNode rr = xx.SelectSingleNode("测试人员");//.SelectSingleNode:取到第一个和表达式匹配的节点
                XmlNodeList ll = rr.ChildNodes; //获取节点的所有子节点
                Form1.de.uiComboBox4.Items.Clear();
                foreach (XmlNode item in ll)
                {
    
    
                    string name11 = item.Name;
                    Form1.de.uiComboBox4.Items.Add(name11.Substring(1, name11.Length - 1));
                    testname.Add(name11.Substring(1, name11.Length - 1));
                }
                Form1.de.uiComboBox4.Text = GetTestPata().Person1;
                xx.Save(pathname);//修改好了 记得保存 才会生效
            }
            catch (Exception ex)
            {
    
    
                MessageBox.Show(ex.Message);
                throw;
            }

            try
            {
    
    
                reportname.Clear();
                XmlDocument xx = new XmlDocument();
                xx.Load(pathreport);
                XmlNode rr = xx.SelectSingleNode("报告保存");//.SelectSingleNode:取到第一个和表达式匹配的节点
                XmlNodeList ll = rr.ChildNodes; //获取节点的所有子节点
                foreach (XmlNode item in ll)
                {
    
    
                    string name11 = item.Name;
                    reportname.Add(name11);
                }
                xx.Save(pathreport);//修改好了 记得保存 才会生效
            }
            catch (Exception ex)
            {
    
    
                MessageBox.Show(ex.Message);
                throw;
            }

        }

生产厂家indexchange

        private void uiComboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
    
    
            
            log.SaveLog("ComboBox1 index");
            string[] nameBuff = new string[500];

            uiComboBox2.Items.Clear();
            Array.Clear(nameBuff, 0, nameBuff.Length);
            nameBuff = SeakName(ref xinghao, uiComboBox1.Text);
            uiComboBox2.Text = nameBuff[0];
            for (int jj = 0; jj < nameBuff.Length; jj++)
            {
    
    
                if (string.IsNullOrEmpty(nameBuff[jj]))
                {
    
    
                    continue;
                }
                uiComboBox2.Items.Add(nameBuff[jj]);
                log.SaveLog("index:uiComboBox2.Items:" + nameBuff[jj]);
            }

            Array.Clear(nameBuff, 0, nameBuff.Length);
            uiComboBox3.Items.Clear();
            nameBuff = SeakName(ref xvlie, uiComboBox1.Text, uiComboBox2.Text);
            uiComboBox3.Text = nameBuff[0];
            for (int jjj = 0; jjj < nameBuff.Length; jjj++)
            {
    
    
                if (string.IsNullOrEmpty(nameBuff[jjj]))
                {
    
    
                    continue;
                }
                uiComboBox3.Items.Add(nameBuff[jjj]);
                log.SaveLog("index:uiComboBox3.Items:" + nameBuff[jjj]);
            }
        }

规格型号indexchange

        private void uiComboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
    
    
            log.SaveLog("ComboBox2 index");
            string[] nameBuff = new string[500];
            Array.Clear(nameBuff, 0, nameBuff.Length);

            uiComboBox3.Items.Clear();
            Array.Clear(nameBuff,0, nameBuff.Length);
            nameBuff = SeakName(ref xvlie, uiComboBox1.Text, uiComboBox2.Text);
            uiComboBox3.Text = nameBuff[0];
            for (int jj = 0; jj < nameBuff.Length; jj++)
            {
    
    
                if (string.IsNullOrEmpty(nameBuff[jj]))
                {
    
    
                    continue;
                }
                uiComboBox3.Items.Add(nameBuff[jj]);
            }
        }

下一步

也就是比对新增功能

       private void uiButton9_Click(object sender, EventArgs e)
        {
    
    
            string[] nameBuff=new string[500];
            string[] nameBuff2 = new string[500];
            Array.Clear(nameBuff, 0, nameBuff.Length);
            Array.Clear(nameBuff2, 0, nameBuff.Length);
            bool add  = true;
            bool add2 = true;
            bool add3 = true;
            bool add4 = true;
            if (WhetherEmpty()==true)
            {
    
    
                XmlDocument xDoc = new XmlDocument();
                xDoc.Load(pathxml);
                for (int i = 0; i < changjia.Count; i++)
                {
    
    
                    //修改
                    if (uiComboBox1.Text == changjia[i])
                    {
    
    
                        nameBuff = SeakName(ref xinghao, uiComboBox1.Text);                    
                        for (int ii = 0; ii < nameBuff.Length; ii++)
                        {
    
    
                            if (string.IsNullOrEmpty(nameBuff[ii]))
                            {
    
    
                                continue;
                            }
                            if (nameBuff[ii]== uiComboBox2.Text)
                            {
    
    
                                //判断第三项是否相等
                                nameBuff2 = SeakName(ref xvlie, uiComboBox1.Text,uiComboBox2.Text);                          
                                for (int iii = 0; iii < nameBuff2.Length; iii++)
                                {
    
    
                                    if (nameBuff2[iii] ==uiComboBox3.Text)
                                    {
    
    
                                        //什么都不用做
                                        add3=false; 
                                        break;
                                    }
                                }
                                if (add3==true)
                                {
    
                                     
                                    //增加一个第三项
                                    string xpath = "气腹机/" + "一"+uiComboBox1.Text +"/"+ "一"+uiComboBox2.Text;
                                    XmlNode newnode = xDoc.SelectSingleNode(xpath);//取指定的单个结点
                                    XmlElement newmember = xDoc.CreateElement("一"+uiComboBox3.Text);
                                    newnode.AppendChild(newmember);
                                    xDoc.Save(pathxml);
                                }
                                add2= false;
                                break;
                            }
                        }
                        if (add2==true)
                        {
    
    
                            //新增一个第二项
                            string xpath = "气腹机/" + "一" + uiComboBox1.Text;
                            XmlNode newnode = xDoc.SelectSingleNode(xpath);
                            XmlElement newmember = xDoc.CreateElement("一" + uiComboBox2.Text);
                            newnode.AppendChild(newmember);
                            XmlElement newmember1 = xDoc.CreateElement("一" + uiComboBox3.Text);
                            newmember.AppendChild(newmember1);
                            xDoc.Save(pathxml);
                        }
                        add= false;
                        break;
                    }
                }
                if (add==true)
                {
    
    
                    新增一列
                    //XmlDocument xDoc = new XmlDocument();
                    //xDoc.Load(pathxml);

                    XmlElement root = xDoc.DocumentElement;

                    //第一级子节点
                    XmlElement FirstNode1 = xDoc.CreateElement("一"+uiComboBox1.Text);
                    root.AppendChild(FirstNode1);

                    //添加二级子节点
                    XmlElement TwoNode = xDoc.CreateElement("一" + uiComboBox2.Text);
                    FirstNode1.AppendChild(TwoNode);

                    //添加三级子节点
                    XmlElement ThreeNode = xDoc.CreateElement("一" + uiComboBox3.Text);
                    TwoNode.AppendChild(ThreeNode);

                    xDoc.Save(pathxml);
                }

                XmlDocument xx = new XmlDocument();
                xx.Load(pathname);
                
                for (int n = 0; n < testname.Count; n++)
                {
    
    
                    if (testname[n] == uiComboBox4.Text)
                    {
    
    
                        //什么都不用做
                        add4 = false;
                        break;
                    }
                }
                if (add4 == true)
                {
    
    
                    XmlNode rr = xx.SelectSingleNode("测试人员");
                    XmlElement newname = xx.CreateElement("一"+ uiComboBox4.Text);
                    rr.AppendChild(newname);
                }

                xx.Save(pathname);//修改好了 记得保存 才会生效

                GetTestPata().Manufacturer1= uiComboBox1.Text;
                GetTestPata().Type1= uiComboBox2.Text;
                GetTestPata().Serial1= uiComboBox3.Text;
                GetTestPata().Person1= uiComboBox4.Text;
                ArgCfgXmlObj.SetPtr();

                Program.mainObj.but();
                Form1.loadform(new TestProj());

                intRefresh();

                //GetTestPata().Person1 = Person[0];
                //GetTestPata().Person2 = Person[1];
                //GetTestPata().Person3 = Person[2];
                //GetTestPata().Person4 = Person[3];
                //ArgCfgXmlObj.SetPtr();
            }
        }

界面2:删除界面

       private void UserSet_Load(object sender, EventArgs e)
        {
    
    
            mulupath = DeviceEntering.GetTestPata().Savemulu;
            textBox1.Text = DeviceEntering.GetTestPata().Savemulu;

            textBox2.Text = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");

            uiComboBox1.Text= reportFile;
            
            foreach(string item in DeviceEntering.reportname)
            {
    
    
                uiComboBox1.Items.Add(item);
            }

            load();
            load2();

        }
      public void load()
        {
    
    
            string[] nameBuff = new string[500];
            DeviceEntering.intRefresh();
            uiComboBox4.Items.Clear();
            foreach (var item in DeviceEntering.changjia)
            {
    
    
                uiComboBox4.Items.Add(item);
            }
            uiComboBox4.Text = DeviceEntering.changjia[0];

            Array.Clear(nameBuff, 0, nameBuff.Length);
            nameBuff = DeviceEntering.SeakName(ref DeviceEntering.xinghao, uiComboBox4.Text);

            uiComboBox7.Items.Clear();
            uiComboBox7.Text = nameBuff[0];
            for (int jj = 0; jj < nameBuff.Length; jj++)
            {
    
    
                if (string.IsNullOrEmpty(nameBuff[jj]))
                {
    
    
                    continue;
                }
                uiComboBox7.Items.Add(nameBuff[jj]);
            }

            Array.Clear(nameBuff, 0, nameBuff.Length);
            nameBuff = DeviceEntering.SeakName(ref DeviceEntering.xvlie, uiComboBox4.Text, uiComboBox7.Text);
            uiComboBox8.Items.Clear();
            uiComboBox8.Text = nameBuff[0];
            for (int jjj = 0; jjj < nameBuff.Length; jjj++)
            {
    
    
                if (string.IsNullOrEmpty(nameBuff[jjj]))
                {
    
    
                    continue;
                }
                uiComboBox8.Items.Add(nameBuff[jjj]);
                //log.SaveLog("load:uiComboBox3.Items:" + nameBuff[jjj]);
            }


        }

        public void load2()
        {
    
    
            //该方法是判断该路径下有没有该文件夹,注意并不能判断某个文件是否存在
            if (!Directory.Exists(DeviceEntering.pathmulu))
            {
    
    
                DirectoryInfo directoryInfo = new DirectoryInfo(DeviceEntering.pathmulu);
                directoryInfo.Create();
            }
            if (!File.Exists(DeviceEntering.pathname))
            {
    
    
                XmlDocument Doc = new XmlDocument();
                XmlDeclaration xnode = Doc.CreateXmlDeclaration("1.0", "UTF-8", "");
                Doc.AppendChild(xnode);

                XmlElement root = Doc.CreateElement("测试人员");
                Doc.AppendChild(root);

                XmlElement TestName = Doc.CreateElement("一测试员");
                root.AppendChild(TestName);
                TestName.InnerText = "张三";
                Doc.Save(DeviceEntering.pathname);
            }

            XmlDocument xx = new XmlDocument();
            xx.Load(DeviceEntering.pathname);
            XmlNode rr = xx.SelectSingleNode("测试人员");//.SelectSingleNode:取到第一个和表达式匹配的节点
            XmlNodeList ll = rr.ChildNodes; //获取节点的所有子节点
            uiComboBox5.Items.Clear();
            foreach (XmlNode item in ll)
            {
    
    
                string name = item.Name;
                uiComboBox5.Items.Add(name.Substring(1,name.Length-1));
                DeviceEntering.testname.Add(name.Substring(1, name.Length - 1));
            }
            uiComboBox5.Text = DeviceEntering.testname[0];
            xx.Save(DeviceEntering.pathname);//修改好了 记得保存 才会生效
        }

生产厂家删除、清空

删除

            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(DeviceEntering.pathxml);

            string xpath = "气腹机" ;    
            string pathChild = "气腹机/" +"一"+ uiComboBox4.Text;

            //XmlNode newnode = xDoc.SelectSingleNode(xpath);
            XmlNode newnode = xDoc.DocumentElement;//取到根结点 就等于上面这样写
            XmlNode chilenode = xDoc.SelectSingleNode(pathChild);
            newnode.RemoveChild(chilenode);
            xDoc.Save(DeviceEntering.pathxml);

            load();

清空

        private void uiButton6_Click(object sender, EventArgs e)
        {
    
    
            File.Delete(DeviceEntering.pathxml);

            load();
        }

规格型号删除、清空

删除

        private void uiButton9_Click(object sender, EventArgs e)
        {
    
    
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(DeviceEntering.pathxml);
            string xpath = "气腹机/" + "一" + uiComboBox4.Text;
            XmlNode newnode = xDoc.SelectSingleNode(xpath);
            //XmlNode newnode = xDoc.DocumentElement;//这里不能这样写了  因为要子集才行
            string pathChild = "气腹机/" + "一" + uiComboBox4.Text + "/" + "一" + uiComboBox7.Text;
            XmlNode chilenode = xDoc.SelectSingleNode(pathChild);
            MessageBox.Show(chilenode.Name);
            newnode.RemoveChild(chilenode);

            xDoc.Save(DeviceEntering.pathxml);
            load();

        }

清空

        private void uiButton8_Click(object sender, EventArgs e)
        {
    
    
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(DeviceEntering.pathxml);

            string xpath = "气腹机/" + "一" + uiComboBox4.Text;
            XmlNode newnode = xDoc.SelectSingleNode(xpath);
            newnode.RemoveAll();

            xDoc.Save(DeviceEntering.pathxml);
            load();

        }

序列号删除清空

删除

        private void uiButton11_Click(object sender, EventArgs e)
        {
    
    
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(DeviceEntering.pathxml);

            string xpath = "气腹机/" + "一" + uiComboBox4.Text + "/" + "一" + uiComboBox7.Text;
            XmlNode newnode = xDoc.SelectSingleNode(xpath);

            string pathChild = "气腹机/" + "一" + uiComboBox4.Text + "/" + "一" + uiComboBox7.Text + "/" + "一" + uiComboBox8.Text;
            XmlNode chilenode = xDoc.SelectSingleNode(pathChild);

            newnode.RemoveChild(chilenode);

            xDoc.Save(DeviceEntering.pathxml);

            load();
        }

清空

        private void uiButton10_Click(object sender, EventArgs e)
        {
    
    
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(DeviceEntering.pathxml);

            string pathChild = "气腹机/" + "一" + uiComboBox4.Text + "/" + "一" + uiComboBox7.Text ;
            XmlNode chilenode = xDoc.SelectSingleNode(pathChild);

            chilenode.RemoveAll();

            xDoc.Save(DeviceEntering.pathxml);

            load();
        }

closed

        private void UserSet_FormClosed(object sender, FormClosedEventArgs e)
        {
    
    
            DeviceEntering.GetTestPata().Manufacturer1 = uiComboBox4.Text;
            DeviceEntering.GetTestPata().Type1 = uiComboBox7.Text;
            DeviceEntering.GetTestPata().Serial1 = uiComboBox8.Text;
            DeviceEntering.GetTestPata().Person1 = uiComboBox5.Text;
            DeviceEntering.GetTestPata().Reportmulu = uiComboBox1.Text;
            DeviceEntering.ArgCfgXmlObj.SetPtr();

            try
            {
    
    
                XmlDocument xx = new XmlDocument();
                xx.Load(DeviceEntering.pathreport);
                bool add = true;
                for (int n = 0; n < DeviceEntering.reportname.Count; n++)
                {
    
    
                    if (DeviceEntering.reportname[n] == uiComboBox1.Text)
                    {
    
    
                        //什么都不用做
                        add = false;
                        break;
                    }
                }
                if (add == true)
                {
    
    
                    XmlNode rr = xx.SelectSingleNode("报告保存");
                    XmlElement newname = xx.CreateElement(uiComboBox1.Text);
                    rr.AppendChild(newname);
                }
                xx.Save(DeviceEntering.pathreport);
            }
            catch (Exception)
            {
    
    

                throw;
            }
            DeviceEntering.intRefresh();

        }

猜你喜欢

转载自blog.csdn.net/chengcao123/article/details/133827915