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:
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:
Thanks for the help! Also if I could get some links to good XML sites please post them.
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.