using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Xml; using System.Text; using System.IO; using System.Windows.Forms; using Mesnac.Equips.BaseInfo; namespace Mesnac.Equips.SetConfig.DataConfig { public partial class DataFieldForm : Form { public DataFieldForm(string equipRunName) { InitializeComponent(); PlcSchema.Instance.ProjectPath = new FileInfo(Factory.Instance.ConfigFile).Directory.FullName; this.textBoxEquip.Text = equipRunName; this.comboBox1.Items.Clear(); this.comboBox1.Items.Add("PmtRecipe"); this.comboBox1.SelectedIndex = 0; } private void button1_Click(object sender, EventArgs e) { if (string.IsNullOrWhiteSpace(textBoxField.Text)) { MessageBox.Show("请设置字段名!"); return; } PlcSchema.Instance.UpdateNodeValueToPlcSchema(textBoxEquip.Text, textBoxField.Text); MessageBox.Show("配置成功!"); this.Close(); } } public class PlcSchema { private string _projectPath = String.Empty; //组态工程路径 private string _PlcSchemaFilePath = "PlcSchema.xml"; //运行结构配置文件路径 /// /// 组态工程路径 /// public string ProjectPath { get { return this._projectPath; } set { this._projectPath = value; } } #region 单例模式 private static PlcSchema _this; public static PlcSchema Instance { get { if (null == _this) _this = new PlcSchema(); return _this; } } #endregion #region 基本信息 public string FilePath() { return Path.Combine(this._projectPath, this._PlcSchemaFilePath); } #endregion #region 解析运行环境主配置文件PlcSchema.xml /// /// 更新配置节点至文件 /// /// /// /// public void UpdateNodeValueToPlcSchema(string equip, string field) { try { string PlcSchemaFile = FilePath(); if (File.Exists(PlcSchemaFile)) { XmlDocument doc = new XmlDocument(); doc.Load(PlcSchemaFile); XmlNode nodeRecipe = doc.SelectSingleNode("//configuration//PlcRecipeWrite//PmtRecipe"); if (nodeRecipe == null) { return; } XmlNodeList nodeList = nodeRecipe.SelectNodes(".//add[@equip]"); bool flag = false; foreach (XmlNode node in nodeList) { if (node.Attributes["equip"].Value == equip) { node.Attributes["field"].Value = field; flag = true; break; } } if (!flag) { XmlElement eAdd = doc.CreateElement("add"); XmlAttribute eAttKey = doc.CreateAttribute("equip"); eAttKey.Value = equip; XmlAttribute eAttValue = doc.CreateAttribute("field"); eAttValue.Value = field; eAdd.Attributes.Append(eAttKey); eAdd.Attributes.Append(eAttValue); nodeRecipe.AppendChild(eAdd); } doc.Save(PlcSchemaFile); } else { //配置文件不存在 ICSharpCode.Core.LoggingService.Error("应用程序配置文件不存在!"); } } catch (Exception ex) { ICSharpCode.Core.LoggingService.Error(ex.Message); throw ex; } } #endregion } }