/*********************************************************************** * File : Service.cs * Purpose : Process Service data * * Author(s) : Christopher Thilges * Last Update : 04/01/2006 * * Change Log : * Version Date Name Description * * Warning: xml file must have , not the iso encoding version: ISO8859-1. * The entire xml file besides the encoding must be enclosed within a braces: <> * Helpful Article by James Divine: * http://www.c-sharpcorner.com/UploadFile/jdivine/XMLTreeView11282005035636AM/XMLTreeView.aspx?ArticleID=9b2fee3a-55b9-4abb-b74a-02dc17e229ba * * 0.1 04/01/06 Chris Thilges - fixed Xml schema * * 0.0 03/22/06 Chris Thilges - wrote class * **********************************************************************/ using System; using System.Drawing; using System.Collections; using System.Windows.Forms; using System.Data; using System.Xml; using System.Text; using System.ComponentModel; using System.Threading; namespace BoilerMap_Cellphone.UI.Forms { public class Service : System.Windows.Forms.Form, IDisposable { private XmlDocument serviceXml; public event finished done; public delegate void finished(); private System.Windows.Forms.TreeView tvTreeView; private System.Windows.Forms.MainMenu mnuMainMenu; private TreeNode branch; public Service(XmlDocument serviceXml) { this.serviceXml = serviceXml; branch = null; InitializeComponent(); } private void updateTreeView() { XmlDocument tmpxmldoc = serviceXml; FillTree(tmpxmldoc.DocumentElement, tvTreeView.Nodes); } private void FillTree(XmlNode node, TreeNodeCollection parentnode) { // End recursion if the node is a text type if (node == null) // || node.NodeType == XmlNodeType.Text || node.NodeType == XmlNodeType.CDATA return; TreeNodeCollection tmptreenodecollection = AddNodeToTree(node, parentnode); // Add all the children of the current node to the treeview foreach (XmlNode tmpchildnode in node.ChildNodes) { FillTree(tmpchildnode, tmptreenodecollection); } } private TreeNodeCollection AddNodeToTree(XmlNode node, TreeNodeCollection parentnode) { TreeNode newchildnode = CreateTreeNodeFromXmlNode(node); // if nothing to add, return the parent item if (newchildnode == null) return parentnode; // add the newly created tree node to its parent if (parentnode != null) parentnode.Add(newchildnode); return newchildnode.Nodes; } private TreeNode CreateTreeNodeFromXmlNode(XmlNode node) { TreeNode tmptreenode = null; //Behavior modifications from Article implementation follow to improve user experience. if ((node.HasChildNodes) && (node.FirstChild.Value != null)) { //tmptreenode = new TreeNode(node.Name); foreach (XmlNode cNode in node.ChildNodes) { if (cNode.Value != null) { TreeNode tmptreenode2 = new TreeNode(cNode.Value); if (branch == null) { tvTreeView.Nodes.Add(tmptreenode2); } else { branch.Nodes.Add(tmptreenode2); } } } } else if (node.NodeType != XmlNodeType.CDATA) { if (node.Attributes != null && node.Attributes["value"] != null && node.Attributes["value"].Value != null) { tmptreenode = new TreeNode(node.Attributes["value"].Value); branch = tmptreenode; } } return tmptreenode; } protected override void Dispose(bool disposing) { base.Dispose(disposing); } private void InitializeComponent() { this.mnuMainMenu = new System.Windows.Forms.MainMenu(); this.tvTreeView = new System.Windows.Forms.TreeView(); this.tvTreeView.Indent = 20; this.tvTreeView.Location = new System.Drawing.Point(0, 0); this.tvTreeView.Size = new System.Drawing.Size(176, 180); updateTreeView(); this.ClientSize = new System.Drawing.Size(176, 180); this.Controls.Add(this.tvTreeView); this.Menu = this.mnuMainMenu; } private void listView1_SelectedIndexChanged(object sender, EventArgs e) { } } }