123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
-
- using System;
- using System.Collections.Generic;
- using System.Windows.Forms;
- using System.Xml;
- namespace EasyModbusClient
- {
- internal class TreeExXMLCls
- {
- private TreeView thetreeview;
- private string xmlfilepath;
- private XmlDocument textdoc;
- public TreeExXMLCls()
- {
- this.textdoc = new XmlDocument();
- }
- ~TreeExXMLCls()
- {
- }
- public List<Node> XMLToList(string XMLFilePath)
- {
- this.xmlfilepath = XMLFilePath;
- this.textdoc.Load(this.xmlfilepath);
- XmlNode xmlNode = this.textdoc.SelectSingleNode("Sinumerik");
- List<Node> list = new List<Node>();
- foreach (XmlNode childNode in xmlNode.ChildNodes)
- {
- Node node = new Node();
- XmlElement xmlElement = (XmlElement)childNode;
- node.Title = xmlElement.GetAttribute("Title");
- node.Key = xmlElement.GetAttribute("Key");
- node.Value = xmlElement.GetAttribute("Value");
- node.ReadType = xmlElement.GetAttribute("ReadType");
- node.Address = xmlElement.GetAttribute("Address").Replace("$", "\"");
- list.Add(node);
- this.TransOrderXml(node, childNode.ChildNodes);
- }
- return list;
- }
- private int TransOrderXml(Node partrenod, XmlNodeList childNodes)
- {
- foreach (XmlNode childNode in childNodes)
- {
- Node node = new Node();
- XmlElement xmlElement = (XmlElement)childNode;
- node.Title = xmlElement.GetAttribute("Title");
- node.Key = xmlElement.GetAttribute("Key");
- node.TargetValue = xmlElement.GetAttribute("TargetValue");
- node.Value = xmlElement.GetAttribute("Value");
- node.ReadType = xmlElement.GetAttribute("ReadType");
- node.Address = xmlElement.GetAttribute("Address").Replace("$", "\"");
- node.CheckAddress = xmlElement.GetAttribute("CheckAddress") == null ? "" : xmlElement.GetAttribute("CheckAddress").Replace("$", "\"");
- if (string.IsNullOrEmpty(xmlElement.GetAttribute("IsAsk")))
- {
- node.IsAsk = false;
- }
- else
- {
- node.IsAsk = Convert.ToBoolean(xmlElement.GetAttribute("IsAsk"));
- }
- node.ChannelNo = xmlElement.GetAttribute("ChannelNo");
- this.TransXML(childNode.ChildNodes, node);
- partrenod.Childs.Add(node);
- }
- return 0;
- }
- private int TransXML(XmlNodeList Xmlnodes, Node partrenod)
- {
- foreach (XmlNode xmlnode in Xmlnodes)
- {
- Node node = new Node();
- XmlElement xmlElement = (XmlElement)xmlnode;
- node.Title = xmlElement.GetAttribute("Title");
- node.Key = xmlElement.GetAttribute("Key");
- node.ReadType = xmlElement.GetAttribute("ReadType");
- node.Address = xmlElement.GetAttribute("Address").Replace("$", "\"");
- node.TargetValue = xmlElement.GetAttribute("TargetValue") == null ? "" : xmlElement.GetAttribute("TargetValue").Replace("$", "\"");
- node.CheckAddress = xmlElement.GetAttribute("CheckAddress");
- if (string.IsNullOrEmpty(xmlElement.GetAttribute("IsBinary")))
- {
- node.IsBinary = false;
- }
- else
- {
- node.IsBinary = Convert.ToBoolean(xmlElement.GetAttribute("IsBinary"));
- }
- if (string.IsNullOrEmpty(xmlElement.GetAttribute("StartPos")))
- {
- node.StartPos = 0;
- }
- else
- {
- node.StartPos = Convert.ToInt32(xmlElement.GetAttribute("StartPos"));
- }
- if (string.IsNullOrEmpty(xmlElement.GetAttribute("EndPos")))
- {
- node.EndPos = 0;
- }
- else
- {
- node.EndPos = Convert.ToInt32(xmlElement.GetAttribute("EndPos"));
- }
- partrenod.Childs.Add(node);
- }
- return 0;
- }
- public int XMLToTree(string XMLFilePath, TreeView TheTreeView)
- {
- this.thetreeview = TheTreeView;
- this.xmlfilepath = XMLFilePath;
- this.textdoc.Load(this.xmlfilepath);
- XmlNode xmlNode = this.textdoc.SelectSingleNode("Sinumerik");
- TreeNode node = new TreeNode();
- node.Text = "Sinumerik";
- this.thetreeview.Nodes.Add(node);
- foreach (XmlNode childNode in xmlNode.ChildNodes)
- {
- TreeNode treeNode = new TreeNode();
- XmlElement xmlElement = (XmlElement)childNode;
- treeNode.Text = xmlElement.GetAttribute("Title");
- treeNode.ToolTipText = xmlElement.GetAttribute("Description");
- node.Nodes.Add(treeNode);
- this.TransXML(childNode.ChildNodes, treeNode);
- }
- return 0;
- }
- private int TransXML(XmlNodeList Xmlnodes, TreeNode partrenod)
- {
- foreach (XmlNode xmlnode in Xmlnodes)
- {
- TreeNode treeNode = new TreeNode();
- XmlElement xmlElement = (XmlElement)xmlnode;
- treeNode.Text = xmlElement.GetAttribute("Title");
- treeNode.ToolTipText = xmlElement.GetAttribute("Description");
- treeNode.Tag = (object)xmlElement.GetAttribute("nodeClass");
- partrenod.Nodes.Add(treeNode);
- if (xmlnode.ChildNodes.Count > 0)
- this.TransXML(xmlnode.ChildNodes, treeNode);
- }
- return 0;
- }
- }
- public class Node
- {
- public string Title
- { get; set; }
- public string Key
- { get; set; }
- public string Value
- { get; set; }
- public string Address
- { get; set; }
- public string TargetValue
- { get; set; }
- public string ReadType
- { get; set; }
- public string ChannelNo
- { get; set; }
-
- public bool IsAsk
- { get; set; }
- public string CheckAddress
- { get; set; }
- public bool IsBinary
- { get; set; }
- public int StartPos
- { get; set; }
- public int EndPos
- { get; set; }
- public List<Node> Childs { get; set; } = new List<Node>();
- }
- }
|