Beispiel #1
0
        private void saveParamXml_Click(object sender, EventArgs e)
        {
            if (refreshParmList.ToArray().Length == 0)
            {
                MessageBox.Show("至少添加一个参数");
                return;
            }
            XmlDocument xmlDoc = new XmlDocument();
            //XML的声明<?xml version="1.0" encoding="gb2312"?>
            XmlDeclaration xmlSM = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null);

            //追加xmldecl位置
            xmlDoc.AppendChild(xmlSM);
            //添加一个名为Gen的根节点
            XmlElement xml = xmlDoc.CreateElement("", "ParamTable", "");

            //追加Gen的根节点位置
            xmlDoc.AppendChild(xml);
            //添加另一个节点,与Gen所匹配,查找<Gen>
            XmlNode gen = xmlDoc.SelectSingleNode("ParamTable");
            //添加一个名为<Version>的节点
            XmlElement ver = xmlDoc.CreateElement("Version");

            //为<Version>节点的属性
            ver.SetAttribute("version", ConfigLoader.GetInstance().FirmwareVersion);
            gen.AppendChild(ver);
            XmlElement p = xmlDoc.CreateElement("ParameterList");

            //循环遍历paramrefreshlist 创建节点
            foreach (string name in refreshParmList)
            {
                XmlElement xele    = xmlDoc.CreateElement("Parameter");
                int        id      = ParamInfoList.ParamInfo_list[name].ID;
                string     opcName = ParamInfoList.ParamInfo_list[name].OpcName;
                xele.InnerText = name;
                xele.SetAttribute("ID", id.ToString());
                xele.SetAttribute("opcName", opcName);
                p.AppendChild(xele);
            }
            gen.AppendChild(p);
            SaveFile_Dialog sf_dialog = new Dialog.SaveFile_Dialog();
            DialogResult    result    = sf_dialog.ShowDialog();

            if (result == DialogResult.OK)
            {
                string filename      = sf_dialog.FileName;
                string save_filepath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
                                                              @"Data\ParamFile\");
                xmlDoc.Save(save_filepath + filename + ".xml");
            }
            sf_dialog.Dispose();
        }