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 XMLToList(string XMLFilePath) { this.xmlfilepath = XMLFilePath; this.textdoc.Load(this.xmlfilepath); XmlNode xmlNode = this.textdoc.SelectSingleNode("Sinumerik"); List list = new List(); 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 Childs { get; set; } = new List(); } }