TreeExXMLCls.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. 
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Windows.Forms;
  5. using System.Xml;
  6. namespace EasyModbusClient
  7. {
  8. internal class TreeExXMLCls
  9. {
  10. private TreeView thetreeview;
  11. private string xmlfilepath;
  12. private XmlDocument textdoc;
  13. public TreeExXMLCls()
  14. {
  15. this.textdoc = new XmlDocument();
  16. }
  17. ~TreeExXMLCls()
  18. {
  19. }
  20. public List<Node> XMLToList(string XMLFilePath)
  21. {
  22. this.xmlfilepath = XMLFilePath;
  23. this.textdoc.Load(this.xmlfilepath);
  24. XmlNode xmlNode = this.textdoc.SelectSingleNode("Sinumerik");
  25. List<Node> list = new List<Node>();
  26. foreach (XmlNode childNode in xmlNode.ChildNodes)
  27. {
  28. Node node = new Node();
  29. XmlElement xmlElement = (XmlElement)childNode;
  30. node.Title = xmlElement.GetAttribute("Title");
  31. node.Key = xmlElement.GetAttribute("Key");
  32. node.Value = xmlElement.GetAttribute("Value");
  33. node.ReadType = xmlElement.GetAttribute("ReadType");
  34. node.Address = xmlElement.GetAttribute("Address").Replace("$", "\"");
  35. list.Add(node);
  36. this.TransOrderXml(node, childNode.ChildNodes);
  37. }
  38. return list;
  39. }
  40. private int TransOrderXml(Node partrenod, XmlNodeList childNodes)
  41. {
  42. foreach (XmlNode childNode in childNodes)
  43. {
  44. Node node = new Node();
  45. XmlElement xmlElement = (XmlElement)childNode;
  46. node.Title = xmlElement.GetAttribute("Title");
  47. node.Key = xmlElement.GetAttribute("Key");
  48. node.TargetValue = xmlElement.GetAttribute("TargetValue");
  49. node.Value = xmlElement.GetAttribute("Value");
  50. node.ReadType = xmlElement.GetAttribute("ReadType");
  51. node.Address = xmlElement.GetAttribute("Address").Replace("$", "\"");
  52. node.CheckAddress = xmlElement.GetAttribute("CheckAddress") == null ? "" : xmlElement.GetAttribute("CheckAddress").Replace("$", "\"");
  53. if (string.IsNullOrEmpty(xmlElement.GetAttribute("IsAsk")))
  54. {
  55. node.IsAsk = false;
  56. }
  57. else
  58. {
  59. node.IsAsk = Convert.ToBoolean(xmlElement.GetAttribute("IsAsk"));
  60. }
  61. node.ChannelNo = xmlElement.GetAttribute("ChannelNo");
  62. this.TransXML(childNode.ChildNodes, node);
  63. partrenod.Childs.Add(node);
  64. }
  65. return 0;
  66. }
  67. private int TransXML(XmlNodeList Xmlnodes, Node partrenod)
  68. {
  69. foreach (XmlNode xmlnode in Xmlnodes)
  70. {
  71. Node node = new Node();
  72. XmlElement xmlElement = (XmlElement)xmlnode;
  73. node.Title = xmlElement.GetAttribute("Title");
  74. node.Key = xmlElement.GetAttribute("Key");
  75. node.ReadType = xmlElement.GetAttribute("ReadType");
  76. node.Address = xmlElement.GetAttribute("Address").Replace("$", "\"");
  77. node.TargetValue = xmlElement.GetAttribute("TargetValue") == null ? "" : xmlElement.GetAttribute("TargetValue").Replace("$", "\"");
  78. node.CheckAddress = xmlElement.GetAttribute("CheckAddress");
  79. if (string.IsNullOrEmpty(xmlElement.GetAttribute("IsBinary")))
  80. {
  81. node.IsBinary = false;
  82. }
  83. else
  84. {
  85. node.IsBinary = Convert.ToBoolean(xmlElement.GetAttribute("IsBinary"));
  86. }
  87. if (string.IsNullOrEmpty(xmlElement.GetAttribute("StartPos")))
  88. {
  89. node.StartPos = 0;
  90. }
  91. else
  92. {
  93. node.StartPos = Convert.ToInt32(xmlElement.GetAttribute("StartPos"));
  94. }
  95. if (string.IsNullOrEmpty(xmlElement.GetAttribute("EndPos")))
  96. {
  97. node.EndPos = 0;
  98. }
  99. else
  100. {
  101. node.EndPos = Convert.ToInt32(xmlElement.GetAttribute("EndPos"));
  102. }
  103. partrenod.Childs.Add(node);
  104. }
  105. return 0;
  106. }
  107. public int XMLToTree(string XMLFilePath, TreeView TheTreeView)
  108. {
  109. this.thetreeview = TheTreeView;
  110. this.xmlfilepath = XMLFilePath;
  111. this.textdoc.Load(this.xmlfilepath);
  112. XmlNode xmlNode = this.textdoc.SelectSingleNode("Sinumerik");
  113. TreeNode node = new TreeNode();
  114. node.Text = "Sinumerik";
  115. this.thetreeview.Nodes.Add(node);
  116. foreach (XmlNode childNode in xmlNode.ChildNodes)
  117. {
  118. TreeNode treeNode = new TreeNode();
  119. XmlElement xmlElement = (XmlElement)childNode;
  120. treeNode.Text = xmlElement.GetAttribute("Title");
  121. treeNode.ToolTipText = xmlElement.GetAttribute("Description");
  122. node.Nodes.Add(treeNode);
  123. this.TransXML(childNode.ChildNodes, treeNode);
  124. }
  125. return 0;
  126. }
  127. private int TransXML(XmlNodeList Xmlnodes, TreeNode partrenod)
  128. {
  129. foreach (XmlNode xmlnode in Xmlnodes)
  130. {
  131. TreeNode treeNode = new TreeNode();
  132. XmlElement xmlElement = (XmlElement)xmlnode;
  133. treeNode.Text = xmlElement.GetAttribute("Title");
  134. treeNode.ToolTipText = xmlElement.GetAttribute("Description");
  135. treeNode.Tag = (object)xmlElement.GetAttribute("nodeClass");
  136. partrenod.Nodes.Add(treeNode);
  137. if (xmlnode.ChildNodes.Count > 0)
  138. this.TransXML(xmlnode.ChildNodes, treeNode);
  139. }
  140. return 0;
  141. }
  142. }
  143. public class Node
  144. {
  145. public string Title
  146. { get; set; }
  147. public string Key
  148. { get; set; }
  149. public string Value
  150. { get; set; }
  151. public string Address
  152. { get; set; }
  153. public string TargetValue
  154. { get; set; }
  155. public string ReadType
  156. { get; set; }
  157. public string ChannelNo
  158. { get; set; }
  159. public bool IsAsk
  160. { get; set; }
  161. public string CheckAddress
  162. { get; set; }
  163. public bool IsBinary
  164. { get; set; }
  165. public int StartPos
  166. { get; set; }
  167. public int EndPos
  168. { get; set; }
  169. public List<Node> Childs { get; set; } = new List<Node>();
  170. }
  171. }