Click here to Skip to main content
15,879,348 members
Articles / Programming Languages / C#
Article

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 45.9K   1.2K   36   5
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.

Image 1

Introduction

Having read and used the NetronProject's "On tree diagrams and XML" that uses the Lithium Tree Layout control, I was motivated to include a class to handle imported XML. The class uses XpathNavigator to work its way through the XML. It can also be used as an example for someone learning .NET classes as well.

The class first imports the XML, which needs to be valid, and then allows the user to specify, if the attributes are to be shown or not. If the attributes are to be shown then the user can specify that they be included in the node text or as child sub nodes. See the diagram above for the differing views.

ConsumeXml is the standalone class. Here are the steps required to use the class:

C#
try
{
  // Step 1 Load Xml
  ConsumeXml LithiumHighway = new ConsumeXml(Xml);
  // Step 2 Turn on or off attributes.                
  LithiumHighway.AttributesShow = checkBoxAttributesOn.Checked;
  // Step 3 Specify attributes as child nodes or within the node text.      
  LithiumHighway.AttributesAsChildren = checkBoxAttributesIncluded.Checked; 
  // Step 4 Process to the control. // Step 4 Process to the control.
  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);
}

Here is the XML, that was used in the example which has a mix and match of attributes and data nodes:

XML
<?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>

The class ConsumeXml handles the traversal and other items and the display of nodes by trojan horsing the attribute(s) into the node. Thanks to a System.Environment.NewLine inserted after the node text.

My goal was to quickly import XML, which seemed to be lacking in the primary project, for viewing of such data instead of editing it. So, after viewing, your mileage may vary. The test project needs to have the Lithium project included before it can compile correctly. So, install that project first and then add the Side Project as it is called in the Zip into Lithium's solution and then build.

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

 
Questionprint the lithium control Pin
Member 432832831-Dec-07 20:45
Member 432832831-Dec-07 20:45 
Hello!
I use with the lithium control and I am very enjoy,
I want to Know if I can print all Tree ,
if you have an idea I Will Happy
Thanks!!

s

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.