XML Tree

TreasonX

Member
Joined
Apr 16, 2003
Messages
6
Okay Im really new to XML so please forgive me :)

I have written a small class that will populate a Tree View with the
elements from a simple XML file.

I would like the list box to display the XML like so.

-Root
|_ Folder_One
| |_Thing_One
| |_Thing_Two
|_Folder_Two
|_Thing_Three
|_Thing_Four

I can get it to display like this with the following XML File:

Code:
<Root>
    <Folder_One>
            <Thing_One></Thing_One>
            <Thing_Two></Thing_Two>
    </Folder_One>
    <Folder_Two>
             <Thing_Three></Thing_Three>
             <Thing_Four></Thing_Four>
    </Folder_Two>
</root>

Is this the proper XML format for what I am trying to achieve?

Here is a sample of the method used to display the XML in a Tree View:

Code:
		private void BuildTree( XmlNode xmlSourceNode, 
			XmlNode document, TreeNode treeNode )
		{
			// create XmlNodeReader to access XML document
			XmlNodeReader nodeReader = new XmlNodeReader( 
				xmlSourceNode );
         
			// represents current node in DOM tree
			XmlNode currentNode = null;

			// treeNode to add to existing tree
			TreeNode newNode = new TreeNode();

			// references modified node type for CreateNode
			XmlNodeType modifiedNodeType;

			while ( nodeReader.Read() )
			{
				// get current node type
				modifiedNodeType = nodeReader.NodeType;

				// check for EndElement, store as Element
				if ( modifiedNodeType == XmlNodeType.EndElement )
					modifiedNodeType = XmlNodeType.Element;

				// create node copy
				currentNode = copy.CreateNode( modifiedNodeType, 
					nodeReader.Name, nodeReader.NamespaceURI );

				// build tree based on node type
				switch ( nodeReader.NodeType )
				{
						// if Text node, add its value to tree
					case XmlNodeType.Text:
						newNode.Text = nodeReader.Value;
						treeNode.Nodes.Add( newNode );

						// append Text node value to currentNode data
						( ( XmlText ) currentNode ).AppendData( 
							nodeReader.Value );
						document.AppendChild( currentNode );
						break;

						// if EndElement, move up tree
					case XmlNodeType.EndElement:
						document = document.ParentNode;
						treeNode = treeNode.Parent;
						break;

						// if new element, add name and traverse tree
					case XmlNodeType.Element:

						// determine if element contains content
						if ( !nodeReader.IsEmptyElement )
						{
							// assign node text, add newNode as child
							newNode.Text = nodeReader.Name;
							treeNode.Nodes.Add( newNode );

							// set treeNode to last child
							treeNode = newNode;
                
							document.AppendChild( currentNode );
							document = document.LastChild;
						}
						else // do not traverse empty elements
						{
							// assign NodeType string to newNode
							newNode.Text = "Empty";

							treeNode.Nodes.Add( newNode );
							document.AppendChild( currentNode );
						}

						break;
               
						// all other types, display node type
					default:
						newNode.Text = nodeReader.NodeType.ToString();
						treeNode.Nodes.Add( newNode );
						document.AppendChild( currentNode );
						break;
				}  // end switch

				newNode = new TreeNode();
			} // end while

			// update the TreeView control
			treeListing.ExpandAll();
			treeListing.Refresh();

		}// end BuildTree

Thanks for the help! Also if I could get some links to good XML sites please post them.
 
I dont think the XML format matters, as long as you know what it is and your Tree-building code knows about it. Im not really sure what other options you have other than attribute-based XML, which would probably be harder to code an XML-To-TreeView piece of code for.

I didnt really look in detail at your code, as far as whether it will work or not. It sounded like it was working so I didnt see the need.

Keep in mind that XML is only what you want it to be. If you just need a structure that helps you build a tree, then what you have is perfect. If you happen to need more detail, then its up to you to add it to the XML.

What kind of XML sites were you looking for? There are XML standards (for certain types of data), theres XSLT, XML parsing, and 5 million other topics. :)

-Ner
 
Back
Top