123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359 |
- //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方法
- /// <summary>
- /// 判断Xml节点是否存在
- /// </summary>
- 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;
- }
- /// <summary>
- /// 添加Xml节点
- /// </summary>
- /// <param name="name">标识</param>
- /// <param name="value">参数值</param>
- 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)
- {
- //记录日志信息
- }
- }
- /// <summary>
- /// 编辑XML
- /// </summary>
- 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);
- }
- /// <summary>
- /// 修改xml
- /// </summary>
- /// <param name="FilePath">文件地址</param>
- /// <param name="RegName">节点名称</param>
- /// <param name="Name">节点属性名称</param>
- /// <param name="Value">节点属性值</param>
- 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节点数据
- /// <summary>
- /// 获取节点数据
- /// </summary>
- /// <param name="FilePath">文件地址</param>
- /// <param name="RegName">节点名称</param>
- /// <param name="Name">节点属性</param>
- /// <returns></returns>
- 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节点参数
- /// <summary>
- /// 获取XML节点参数
- /// </summary>
- /// <param name="Name">节点参数名称</param>
- /// <returns>节点参数</returns>
- 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节点参数
- /// <summary>
- /// 获取XML节点参数
- /// </summary>
- /// <param name="Name">节点参数名称</param>
- /// <returns>节点参数</returns>
- 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 获取窗体中所有参数
- /// <summary>
- /// 获取窗体中控件的所有参数
- /// </summary>
- 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
- public static bool WriteSysType(string xmlpath, string AttributeName, string Value)
- {
- try
- {
- XmlDocument xmlDocument = new XmlDocument();
- xmlDocument.Load(xmlpath);
- XmlNode node = xmlDocument.SelectSingleNode("XMLData");
- XmlElement xe = (XmlElement)node;
- xe.SetAttribute(AttributeName, Value);
- xmlDocument.Save(xmlpath);
- }
- catch /*(Exception ex)*/
- {
- //MessageBox.Show(ex.ToString());
- return false;
- }
- return true;
- }
- public static bool ReadSysType(string xmlpath, string AttributeName,ref string Value)
- {
- try
- {
- XmlDocument xmlDocument = new XmlDocument();
- xmlDocument.Load(xmlpath);
- XmlNode node = xmlDocument.SelectSingleNode("XMLData");
- XmlElement xe = (XmlElement)node;
- Value=xe.GetAttribute(AttributeName);
- }
- catch /*(Exception ex)*/
- {
- //MessageBox.Show(ex.ToString());
- return false;
- }
- return true;
- }
- }
- }
|