TreasonX Posted April 19, 2003 Posted April 19, 2003 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: <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: 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. Quote
*Experts* Nerseus Posted April 21, 2003 *Experts* Posted April 21, 2003 I don't think the XML format matters, as long as you know what it is and your Tree-building code knows about it. I'm 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 didn't really look in detail at your code, as far as whether it will work or not. It sounded like it was working so I didn't 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 it's 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), there's XSLT, XML parsing, and 5 million other topics. :) -Ner Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.