using System.Collections.Generic; using System.IO; using System.Windows.Forms; using System.Xml; using System.Xml.Linq; namespace UIService { public enum EnumAxisGroupType { AxisGroupS, AxisGroupXy, AxisGroupXyz, AxisGroupXyzu, AxisGroupXyzuvw } public enum EnumPLCAxisGroupType { PLCAxisGroupS, PLCAxisGroupXy, PLCAxisGroupXyz, PLCAxisGroupXyzu, PLCAxisGroupXyzuvw } public class MotionCardConfigInfo { public string CardType { get; set; } = "CardType"; public string CardNo { get; set; } = "CardNo"; public string CANCount { get; set; } = "CANCount"; public string CANBaund { get; set; } = "CANBaund"; public string FilePath { get; set; } = "FilePath"; public string Axises { get; set; } = "Axises"; public string TypeName { get; set; } = "LeadShine"; } public class PLCConfigInfo { public string PLCModelName { get; set; } = "PLCModelName"; public string PLCNo { get; set; } = "PLCNo"; public string CommunicateType { get; set; } = "CommunicateType"; public string IP { get; set; } = "IP"; public string Port { get; set; } = "Port"; public string COM { get; set; } = "COM"; public string Axises { get; set; } = "Axises"; } public class AxisConfigInfo { public string AxisID { set; get; } = "AxisID"; public string AxisName { set; get; } = "AxisName"; public string AxisNo { set; get; } = "AxisNo"; public string AxisDescribtion { set; get; } = "AxisDescribtion"; public string CardName { set; get; } = "CardName"; } public class AxisGroupConfigInfo { public string ID { get; set; } = "ID"; public string AxisGroupName { get; set; } = "AxisGroupName"; public string AxisGroupTypeName { get; set; } = "AxisGroupTypeName"; public List ListGroupAxisConfigInfo = new List(); } public class IOConfigInfo { public string ID { get; set; } = "ID"; public string Name { get; set; } = "Name"; public string CardType { get; set; } = "CardType"; } public class ClsHardConfig { XmlDocument docXml = new XmlDocument(); public void LoadXml(string strPath, TreeView treeView) { if (File.Exists(strPath)) { docXml.Load(strPath); //.TableMananger.xml "TreeXml.xml" RecursionTreeControl(docXml.DocumentElement, treeView.Nodes);//将加载完成的XML文件显示在TreeView控件中 } } /// /// RecursionTreeControl:表示将XML文件的内容显示在TreeView控件中 /// /// 将要加载的XML文件中的节点元素 /// 将要加载的XML文件中的节点集合 public void RecursionTreeControl(XmlNode xmlNode, TreeNodeCollection nodes) { foreach (XmlNode node in xmlNode.ChildNodes)//循环遍历当前元素的子元素集合 { TreeNode new_child = new TreeNode();//定义一个TreeNode节点对象 new_child.Name = node.Attributes["Name"].Value; new_child.Text = node.Attributes["Text"].Value; nodes.Add(new_child);//向当前TreeNodeCollection集合中添加当前节点 RecursionTreeControl(node, new_child.Nodes);//调用本方法进行递归 } } #region Save public void SaveToXml(TreeView treeView,string strPath) { XDeclaration dec = new XDeclaration("1.0", "utf-8", "yes"); XDocument xml = new XDocument(dec); XElement root = new XElement("Tree"); foreach (TreeNode node in treeView.Nodes) { XElement e = CreateElements(node); root.Add(e); } xml.Add(root); xml.Save(strPath);//"TreeXml.xml" } public XElement CreateElements(TreeNode node) { XElement root = CreateElement(node); foreach (TreeNode n in node.Nodes) { XElement e = CreateElements(n); root.Add(e); } return root; } public XElement CreateElement(TreeNode node) { return new XElement("Node", new XAttribute("Name", node.Name), new XAttribute("Text", node.Text) ); } #endregion public List listMotionCardConfigInfo = new List(); public List listCardAxisInfos = new List(); public List listCardGroupInfo = new List(); public List listIOConfigInfo = new List(); public List listPLCAxisInfos = new List(); public List listPLCConfigInfos = new List(); public List listPLCGroupConfigInfo = new List(); public void ReadXml(string strPath) { XmlDocument doc = new XmlDocument(); listCardAxisInfos.Clear(); listMotionCardConfigInfo.Clear(); listCardGroupInfo.Clear(); listIOConfigInfo.Clear(); listPLCAxisInfos.Clear(); listPLCConfigInfos.Clear(); listPLCGroupConfigInfo.Clear(); doc.Load(strPath); XmlNode rootNode = doc.DocumentElement; foreach(XmlNode firstNode in rootNode.ChildNodes)//运动控制卡 / PLC /IO { GetChildNode(firstNode, out string strFirstName, out string strFirstText); switch(strFirstName) { case "运动控制卡": foreach (XmlNode secondNode in firstNode.ChildNodes)//LeadShine,APE,轴组 { GetChildNode(secondNode, out string strSecondName, out string strSecondText); switch (strSecondName) { case "LeadShine": GetMotionCardAndAxisInfo(secondNode, strSecondName); break; case "APE": GetMotionCardAndAxisInfo(secondNode, strSecondName); break; case "轴组": AddAxisGroupConfigInfo(secondNode, listCardGroupInfo); break; } } break; case "PLC": foreach(XmlNode nodePLCName in firstNode.ChildNodes)//欧姆龙,三菱,轴组 { GetChildNode(nodePLCName, out string strPLCName, out string strPLCText); switch(strPLCName) { case "欧姆龙": GetPLCAndAxisInfo(nodePLCName); break; case "三菱": GetPLCAndAxisInfo(nodePLCName); break; case "轴组": AddAxisGroupConfigInfo(nodePLCName, listPLCGroupConfigInfo); break; } } break; case "IO卡": foreach (XmlNode nodeIO in firstNode.ChildNodes)//LeadShine,APE { GetChildNode(nodeIO, out string strIOName, out string strIOText); foreach(XmlNode nodeType in nodeIO)//IOC0640,IOC1280,IOD2424P, { GetChildNode(nodeType, out string strTypeName, out string strTypeText); IOConfigInfo iOConfigInfo = new IOConfigInfo(); iOConfigInfo.Name = strTypeName; iOConfigInfo.CardType = strIOName ; iOConfigInfo.ID = nodeType.ChildNodes[0].ChildNodes[0].Attributes["Text"].Value; listIOConfigInfo.Add(iOConfigInfo); } } break; } } int a = listMotionCardConfigInfo.Count; int b = listCardAxisInfos.Count; int c = listCardGroupInfo.Count; int d = listIOConfigInfo.Count; } public void GetChildNode(XmlNode xmlNode,out string strName,out string strText) { strName = xmlNode.Attributes["Name"].Value; strText = xmlNode.Attributes["Text"].Value; } public void AddAxisConfigInfo(XmlNode xmlNode,List listAxis) { foreach (XmlNode chileNode in xmlNode)//Axis[0],Axis[1],Axis[2],Axis[3], { AxisConfigInfo axisConfigInfo = new AxisConfigInfo(); foreach (XmlNode detailNode in chileNode)//ID,AxisName,AxisNo,Desc,CardName { switch (detailNode.Attributes["Name"].Value) { case "AxisID": axisConfigInfo.AxisID = detailNode.ChildNodes[0].Attributes["Text"].Value; break; case "AxisName": axisConfigInfo.AxisName = detailNode.ChildNodes[0].Attributes["Text"].Value; break; case "AxisNo": axisConfigInfo.AxisNo = detailNode.ChildNodes[0].Attributes["Text"].Value; break; case "AxisDescribtion": axisConfigInfo.AxisDescribtion = detailNode.ChildNodes[0].Attributes["Text"].Value; break; case "CardName": axisConfigInfo.CardName = detailNode.ChildNodes[0].Attributes["Text"].Value; break; } } listAxis.Add(axisConfigInfo); } } public void AddAxisGroupConfigInfo(XmlNode nodeGroup, List listGroup) { AxisGroupConfigInfo axisGroupConfigInfo = new AxisGroupConfigInfo(); foreach (XmlNode groupNode in nodeGroup.ChildNodes)//ID,AxisGroupName,AxisGroupTypeName { GetChildNode(groupNode, out string strGroupName, out string strGroupText); switch (strGroupName) { case "ID": axisGroupConfigInfo.ID = groupNode.ChildNodes[0].Attributes["Text"].Value; break; case "AxisGroupName": axisGroupConfigInfo.AxisGroupName = groupNode.ChildNodes[0].Attributes["Text"].Value; break; case "AxisGroupTypeName": XmlNode groupTypeNode = groupNode.ChildNodes[0];//AxisGroupXy,AxisGroupXyz,AxisGroupS, GetChildNode(groupTypeNode, out string strTypeName, out string strTypeText); axisGroupConfigInfo.AxisGroupTypeName = strTypeName; AddAxisConfigInfo(groupTypeNode, axisGroupConfigInfo.ListGroupAxisConfigInfo); break; } } listGroup.Add(axisGroupConfigInfo); } public void GetMotionCardAndAxisInfo(XmlNode secondNode,string strCardTypeName) { foreach (XmlNode thirdNode in secondNode.ChildNodes)//DMC5400A,DMC5600,DMC5800,DMC5C00 { MotionCardConfigInfo motionCardConfigInfo = new MotionCardConfigInfo(); GetChildNode(thirdNode, out string strThirdName, out string strThirdText); motionCardConfigInfo.CardType = strThirdName; motionCardConfigInfo.TypeName = strCardTypeName; foreach (XmlNode fourthNode in thirdNode.ChildNodes)//CardNo,CANCount,CANBaund,FilePath,Axises { GetChildNode(fourthNode, out string strFourthName, out string strFourthText); switch (strFourthName) { case "CardNo": motionCardConfigInfo.CardNo = fourthNode.ChildNodes[0].Attributes["Text"].Value; break; case "CANCount": motionCardConfigInfo.CANCount = fourthNode.ChildNodes[0].Attributes["Text"].Value; break; case "CANBaund": motionCardConfigInfo.CANBaund = fourthNode.ChildNodes[0].Attributes["Text"].Value; break; case "FilePath": motionCardConfigInfo.FilePath = fourthNode.ChildNodes[0].Attributes["Text"].Value; break; case "Axises": AddAxisConfigInfo(fourthNode, listCardAxisInfos); break; } } listMotionCardConfigInfo.Add(motionCardConfigInfo); } } public void GetPLCAndAxisInfo(XmlNode nodePLCName) { foreach (XmlNode nodePLCModel in nodePLCName.ChildNodes)//CP,NX,NJ { PLCConfigInfo plcConfigInfo = new PLCConfigInfo(); GetChildNode(nodePLCModel, out string strModelName, out string strModelText); plcConfigInfo.PLCModelName = strModelName; foreach (XmlNode nodePLCConfig in nodePLCModel.ChildNodes)//PLCNo,CommunicateType,IP,Port,COM,Axises { GetChildNode(nodePLCConfig, out string strConfigName, out string strConfigText); switch (strConfigName) { case "PLCNo": plcConfigInfo.PLCNo = nodePLCConfig.ChildNodes[0].Attributes["Text"].Value; break; case "CommunicateType": plcConfigInfo.CommunicateType = nodePLCConfig.ChildNodes[0].Attributes["Text"].Value; break; case "IP": plcConfigInfo.IP = nodePLCConfig.ChildNodes[0].Attributes["Text"].Value; break; case "Port": plcConfigInfo.Port = nodePLCConfig.ChildNodes[0].Attributes["Text"].Value; break; case "COM": plcConfigInfo.COM = nodePLCConfig.ChildNodes[0].Attributes["Text"].Value; break; case "Axises": AddAxisConfigInfo(nodePLCConfig, listPLCAxisInfos); break; } } listPLCConfigInfos.Add(plcConfigInfo); } } } }