Click here to Skip to main content
15,892,927 members
Articles / Programming Languages / C#

Add-on class to process XML using XPathNavigator into Lithium Tree Control

Rate me:
Please Sign up or sign in to vote.
3.81/5 (8 votes)
15 Apr 20051 min read 46K   1.2K   36  
This class will take any valid XML and parse out the nodes with or without the attributes into a Lithium Tree Layout control for viewing.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Netron.Lithium;
using System.Xml;
using System.Xml.XPath;
using System.Text;

using ConsumeLithium;

namespace SideProject
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
      private Netron.Lithium.LithiumControl lithiumControl;
      private System.Windows.Forms.GroupBox groupBox1;
      private System.Windows.Forms.CheckBox checkBoxAttributesOn;
      private System.Windows.Forms.CheckBox checkBoxAttributesIncluded;
      private System.Windows.Forms.Button RefreshIt;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

         LoadXml(OTA);
         this.lithiumControl.Root.Expand();
         this.lithiumControl.DrawTree();

      }

      String OTA = @"<?xml version='1.0' encoding='utf-8' ?>
                     <Top CodeProject='online'>
                        <Child index='1'>Child data</Child>
                        <Other Show='f'>Other Data</Other>
                        <Final attr1='This' attr2='is' attr3='Radio' attr4='Clash'/>
                     </Top>";



      private void LoadXml(String Xml)
      {

         try
         {
            ConsumeXml LithiumHighway           = new ConsumeXml(Xml);
            LithiumHighway.AttributesAsChildren = checkBoxAttributesIncluded.Checked;
            LithiumHighway.AttributesShow       = checkBoxAttributesOn.Checked;

            LithiumHighway.Process(this.lithiumControl.Root);

            this.lithiumControl.Root.Move(new Point(20-this.lithiumControl.Root.X, 
                                          Convert.ToInt32(this.lithiumControl.Height/2)-this.lithiumControl.Root.Y));

            // See the Lithium Example for this Node level highlighter, otherwise comment out.			
            ColoringVisitor visitor = new ColoringVisitor(); 

            lithiumControl.BreadthFirstTraversal(visitor);

         }
         catch (System.Exception ex)
         {
            MessageBox.Show("Exception Caught: " + ex.Message);
         }


      }

      ColoringVisitor CV = new ColoringVisitor();

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
         this.lithiumControl = new Netron.Lithium.LithiumControl();
         this.groupBox1 = new System.Windows.Forms.GroupBox();
         this.checkBoxAttributesOn = new System.Windows.Forms.CheckBox();
         this.checkBoxAttributesIncluded = new System.Windows.Forms.CheckBox();
         this.RefreshIt = new System.Windows.Forms.Button();
         this.groupBox1.SuspendLayout();
         this.SuspendLayout();
         // 
         // lithiumControl
         // 
         this.lithiumControl.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
         this.lithiumControl.AutoScroll = true;
         this.lithiumControl.BranchHeight = 120;
         this.lithiumControl.ConnectionType = Netron.Lithium.ConnectionType.Default;
         this.lithiumControl.LayoutDirection = Netron.Lithium.TreeDirection.Horizontal;
         this.lithiumControl.LayoutEnabled = true;
         this.lithiumControl.Location = new System.Drawing.Point(0, 0);
         this.lithiumControl.Name = "lithiumControl";
         this.lithiumControl.Size = new System.Drawing.Size(846, 582);
         this.lithiumControl.TabIndex = 0;
         this.lithiumControl.WordSpacing = 20;
         // 
         // groupBox1
         // 
         this.groupBox1.Controls.Add(this.RefreshIt);
         this.groupBox1.Controls.Add(this.checkBoxAttributesIncluded);
         this.groupBox1.Controls.Add(this.checkBoxAttributesOn);
         this.groupBox1.Location = new System.Drawing.Point(857, 5);
         this.groupBox1.Name = "groupBox1";
         this.groupBox1.Size = new System.Drawing.Size(181, 574);
         this.groupBox1.TabIndex = 1;
         this.groupBox1.TabStop = false;
         this.groupBox1.Text = "Actions";
         // 
         // checkBoxAttributesOn
         // 
         this.checkBoxAttributesOn.Location = new System.Drawing.Point(8, 17);
         this.checkBoxAttributesOn.Name = "checkBoxAttributesOn";
         this.checkBoxAttributesOn.Size = new System.Drawing.Size(155, 24);
         this.checkBoxAttributesOn.TabIndex = 0;
         this.checkBoxAttributesOn.Text = "Attributes On";
         // 
         // checkBoxAttributesIncluded
         // 
         this.checkBoxAttributesIncluded.Location = new System.Drawing.Point(8, 52);
         this.checkBoxAttributesIncluded.Name = "checkBoxAttributesIncluded";
         this.checkBoxAttributesIncluded.Size = new System.Drawing.Size(155, 24);
         this.checkBoxAttributesIncluded.TabIndex = 1;
         this.checkBoxAttributesIncluded.Text = "Attributes As Children";
         // 
         // RefreshIt
         // 
         this.RefreshIt.Location = new System.Drawing.Point(10, 81);
         this.RefreshIt.Name = "RefreshIt";
         this.RefreshIt.TabIndex = 2;
         this.RefreshIt.Text = "Redo";
         this.RefreshIt.Click += new System.EventHandler(this.RefreshIt_Click);
         // 
         // Form1
         // 
         this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
         this.ClientSize = new System.Drawing.Size(1042, 582);
         this.Controls.Add(this.groupBox1);
         this.Controls.Add(this.lithiumControl);
         this.Name = "Form1";
         this.Text = "Form1";
         this.groupBox1.ResumeLayout(false);
         this.ResumeLayout(false);

      }
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

      private void RefreshIt_Click(object sender, System.EventArgs e)
      {
         this.lithiumControl.NewDiagram();
         LoadXml(OTA);
         this.lithiumControl.Root.Expand();
         this.lithiumControl.DrawTree();      
      }

	}


   /// <summary>
   /// This example colors the level in a gradient
   /// which emphasizes the nodes on the same level.
   /// Since the level is stored in the ShapeBase you can use either the 
   /// BFT or the DFT algorithm.
   /// </summary>
   public class ColoringVisitor : IPrePostVisitor
   {
      #region Fields
      public event Messager OnInfo;
      Color[] gradient;
      #endregion

      #region Properties
      public bool IsDone
      {
         get
         {			
            return false;
         }
      }

      #endregion

      #region Constructor
      /// <summary>
      /// Default ctor
      /// </summary>
      public ColoringVisitor()
      {			
         gradient = new Color[10]{
                                    Color.RoyalBlue,
                                    Color.CornflowerBlue,
                                    Color.LightSteelBlue,
                                    Color.SteelBlue,
                                    Color.PowderBlue,
                                    Color.Lavender,
                                    Color.LightCyan,
                                    Color.Azure,
                                    Color.Thistle,
                                    Color.Plum
                                 };

      }
      #endregion

      #region Methods
		
      public void PreVisit(ShapeBase shape)
      {
			
      }

      public  void Visit(ShapeBase shape)
      {
         shape.ShapeColor = gradient[shape.Level%10]; //if above level 9 re-use the array
      }


      public  void Darken(ShapeBase shape)
      {
         shape.ShapeColor = Color.Tomato; //if above level 9 re-use the array
      }

  
      public void PostVisit(ShapeBase shape)
      {
		
      }

      private void RaiseInfo(string msg)
      {
         if(OnInfo!=null)
            OnInfo(msg);
      }

      #endregion

   }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
After finding myself with lots of time after a cataclysmic event I have turned to doing programming to help fill the hours.

Comments and Discussions