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






3.81/5 (8 votes)
Apr 15, 2005
1 min read

46530

1173
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.
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:
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 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.