tags:

views:

277

answers:

3

I have to load an Xml document and display in a tree view in a winform. here is a piece of code. Please tell whats the error.

private void AddingNodesToTree(ref XmlNode xnode, ref TreeNode tnode)
{ 
    TreeNode subNode = treeNodes.Add(xnode.Name);
    subNode.Tag = xnode;
    foreach (XmlNode subElement in xnode.ChildNodes)
    {
        AddingNodesToTree(subNode.Nodes, subElement);
    }
}

after this i select node in that tree to display the attributes of selected node:

private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
    XmlNode xNode = e.Node.Tag as XmlNode;

    if (xNode != null)
    {
        foreach (XmlNode subNode in xNode.ChildNodes)
        {
            listBox1.Items.Add(subNode.Name);
        }
    }
}

WHats possible error in this code? Please rectify.

+1  A: 

Well for one thing, in your recursive call to AddingNodesToTree you're not specifying ref on the arguments. It's not at all clear why they're on the parameters in the first place though. Your arguments to the recursive call also appear to be the wrong type. Is it actually a recursive call, or are you calling an overload which you haven't shown?

The second obvious problem is that you're not using the tnode parameter within AddingNodesToTree. Should this line:

TreeNode subNode = treeNodes.Add(xnode.Name);

actually be this:

TreeNode subNode = tnode.Add(xnode.Name);

Finally, you haven't told us what's actually wrong. Does your code not compile, or not work as you expected? If it's the former, what's the compiler error? If it's the latter, please give details of the actual behaviour vs the expected behaviour.

Jon Skeet
Thanks Jon, that was a typo in the the first function. Arguments got exchanged. The errors that i am getting as a compile error is: Expected class, delegate, enum, interface, or structType or namespace definition, or end-of-file expected
Well without seeing the whole file, that doesn't really help much. It could be anything. You should also check your understanding of "ref" parameters: http://pobox.com/~skeet/csharp/parameters.html
Jon Skeet
In particular, *where* is that compile-time error?
Jon Skeet
A: 

I guess you have a compiler error

private void AddingNodesToTree(ref XmlNode xnode, ref TreeNode tnode)
{ 
    TreeNode subNode = treeNodes.Add(xnode.Name);
    subNode.Tag = xnode;
    foreach (XmlNode subElement in xnode.ChildNodes)
    {
    // you call it with the wrong params?
        AddingNodesToTree(subNode.Nodes, subElement);
    //should be
        AddingNodesToTree(ref subElement, ref subNode);
    }
}
Stormenet
Or better: drop the ref in the parameter list - they are not needed.
Henk Holterman
I was talking about his parameters that were swapped. But you can drop the ref too.
Stormenet
A: 

My guess: You want to show the attributes of the node, so iterate over xNode.Attributes instead of xNode.ChildNodes. Optionally, clear the listview before adding the attributes.

Rauhotz
TreeNode n = e.Node;XmlElement c = (XmlElement)n.Tag;XmlAttributeCollection attCol = c.Attributes; foreach (XmlAttribute xmlatt in attCol){listBox1.Items.Show(xmlatt);}I replaced this with earlier code but gettin error Expected class, delegate, enum, interface, or struct