//using COTSXmlFileDll; using System; using System.Xml; namespace OTSSysMgrApp { public class XMLOperationClass { #region 全部变量声明 //获取XML 路径 static string xmlFilePath = System.Configuration.ConfigurationManager.ConnectionStrings["XMLFilePath"].ConnectionString; #endregion public XMLOperationClass() { } #region 操作XML方法 /// /// 判断Xml节点是否存在 /// public static bool ExistsXmlInfo(string Name) { XmlNode SelectNode = null; XmlDocument doc = new XmlDocument(); doc.Load(xmlFilePath); //加载Xml文件 XmlElement root = doc.DocumentElement; //获取根节点 XmlNodeList mainNodes = root.GetElementsByTagName("parameter"); //获取子节点集合 foreach (XmlNode node in mainNodes) { //获取Name属性值 string name = ((XmlElement)node).GetAttribute("Name"); //获取Age子XmlElement集合 string value = ((XmlElement)node).GetAttribute("Value"); if (name.Equals(Name)) { SelectNode = node; if (SelectNode != null) { return true; } } } return false; } /// /// 添加Xml节点 /// /// 标识 /// 参数值 public static void AddXmlInfo(string Name, string Value) { try { XmlDocument doc = new XmlDocument(); doc.Load(xmlFilePath); XmlElement root = doc.DocumentElement; //根节点的添加独立子节点 XmlElement systemData = doc.CreateElement("parameter"); //设置参数 systemData.SetAttribute("Name", Name); systemData.SetAttribute("Value", Value); //添加至父节点 root.AppendChild(systemData); //保存 doc.Save(xmlFilePath); } catch (Exception) { //记录日志信息 } } /// /// 编辑XML /// public static void EditXmlInfo(string Name, string Value) { XmlDocument doc = new XmlDocument(); doc.Load(xmlFilePath); //加载Xml文件 XmlElement root = doc.DocumentElement; //获取根节点 XmlNodeList personNodes = root.GetElementsByTagName("parameter"); //获取Person子节点集合 foreach (XmlNode node in personNodes) { XmlElement ele = (XmlElement)node; if (ele.GetAttribute("Name") == Name) { ele.Attributes["Value"].Value = Value; } } doc.Save(xmlFilePath); } /// /// 修改xml /// /// 文件地址 /// 节点名称 /// 节点属性名称 /// 节点属性值 public static bool EditXmlInfoWidthPath(string FilePath,string RegName,string Name, string Value) { bool isSuccess = false; XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(FilePath); //加载Xml文件 XmlNodeList nodeList = xmlDoc.GetElementsByTagName("Member"); foreach (XmlNode node in nodeList) { XmlElement ele = (XmlElement)node; if (ele.GetAttribute("RegName") == RegName) { ele.SetAttribute(Name, Value); isSuccess = true; break; } } if (isSuccess) { xmlDoc.Save(FilePath); } return isSuccess; } #endregion #region 获取XML节点数据 /// /// 获取节点数据 /// /// 文件地址 /// 节点名称 /// 节点属性 /// public static string GetXMLReg(string FilePath, string RegName, string Name) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(FilePath); //加载Xml文件 XmlNodeList nodeList = xmlDoc.GetElementsByTagName("Member"); string str = ""; foreach (XmlNode node in nodeList) { XmlElement ele = (XmlElement)node; if (ele.GetAttribute("RegName") == RegName) { str = ele.GetAttribute(Name); break; } } return str; } #endregion #region 获取XML节点参数 /// /// 获取XML节点参数 /// /// 节点参数名称 /// 节点参数 public static XmlNode GetXMLInformation(string Name) { try { XmlNode SelectNode = null; XmlDocument doc = new XmlDocument(); doc.Load(xmlFilePath); //加载Xml文件 XmlElement root = doc.DocumentElement; //获取根节点 XmlNodeList mainNodes = root.GetElementsByTagName("parameter"); //获取子节点集合 foreach (XmlNode node in mainNodes) { //获取Name属性值 string name = ((XmlElement)node).GetAttribute("Name"); //获取Age子XmlElement集合 string value = ((XmlElement)node).GetAttribute("Value"); if (name.Equals(Name)) { SelectNode = node; } } return SelectNode; } catch (Exception) { return null; } } #endregion #region 获取XML节点参数 /// /// 获取XML节点参数 /// /// 节点参数名称 /// 节点参数 public static string GetXMLInformations(string Name) { try { string value = string.Empty; XmlDocument doc = new XmlDocument(); doc.Load(xmlFilePath); //加载Xml文件 XmlElement root = doc.DocumentElement; //获取根节点 XmlNodeList mainNodes = root.GetElementsByTagName("parameter"); //获取子节点集合 foreach (XmlNode node in mainNodes) { //获取Name属性值 string name = ((XmlElement)node).GetAttribute("Name"); if (name.Equals(Name)) { value = ((XmlElement)node).GetAttribute("Value"); } } return value; } catch (Exception) { return ""; } } #endregion #region 获取窗体中所有参数 /// /// 获取窗体中控件的所有参数 /// public static XmlNode GetXMLInformationValue(string selNodeName) { try { XmlNode selectNode = null; XmlDocument doc = new XmlDocument(); //加载Xml文件 doc.Load(xmlFilePath); //获取根节点 XmlElement root = doc.DocumentElement; //获取子节点集合 XmlNodeList mainNodes = root.GetElementsByTagName("parameter"); foreach (XmlNode node in mainNodes) { string NodeName = ((XmlElement)node).GetAttribute("Name"); string NodeValue = ((XmlElement)node).GetAttribute("Value"); if (NodeName == selNodeName) { //获取属性名称与属性值 selectNode = node; break; } } return selectNode; } catch (Exception) { return null; } } #endregion #region 写入COTSXMLFileDllExportForCShape public static bool WriteXMLFile(string FileName,string SEMName, string EDSName) { try { bool isSuccessSem = false; bool isSuccessEds = false; isSuccessSem = EditXmlInfoWidthPath(FileName, "SemControllerName", "Value", SEMName); isSuccessEds = EditXmlInfoWidthPath(FileName, "EDSName", "Value", EDSName); return isSuccessSem && isSuccessEds; } catch (Exception) { return false; } } #endregion #region 读取COTSXMLFileDllExportForCShape public static bool ReadXMLFile(string FileName,ref string SEMName, ref string EDSName) { try { SEMName = GetXMLReg(FileName, "SemControllerName", "Value"); EDSName = GetXMLReg(FileName, "EDSName", "Value"); return true; } catch (Exception) { return false; } } #endregion } }