views:

113

answers:

1

I have a treeView displayed on a winform.Now when i click on an xmlnode in the treeview its attributes get displayed in the listbox.Now i have divided the whole logic in UI and back end part. Now what i want that my back end class contains the method to display atrributes(name and value) of the xml node clicked and these are stored in an array and it is returned as a string to my front end class in the treev_AfterSelect event. How can i do it?I need to store attributes of the node i click on winform in a string array and display in listbox.Here is the code what i have for my Backhand class enter code here

public  string[] selectedNode(XmlNode eventNode)
    {
        XmlAttributeCollection attCol = eventNode.Attributes;
        string[] strArray = new string[attCol.Count];
         if (attCol != null)
            for( int i = 0; i <= attCol.Count;i++)
            {  strArray[i] = "Attribute name: " + attCol[i].Name+","+" Attribute value: " + attCol[i].Value;//IndexOutOfRange Exception
                    }
            return strArray;
    }

Here i am getting an exception of IndexOutOfRangeException : The index being passed in is out of range. on this line

strArray[i] = "Attribute name: " + attCol[i].Name+","+" Attribute value: " + attCol[i].Value;

My Front end ( UI ) class contains this code to retrieve the Attribute and values, display it on Listbox.

private void treeView1_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
    {
        classObj = new MytreeNodeClass();
       listBox1.Items.Clear();
        XmlNode xNode = e.Node.Tag as XmlNode;
        string[] arrStr = classObj.selectedNode(xNode); 
        listBox1.Items.Add(arrStr); //Is this the correct syntax to retrieve the data in listbox??
     }

Can you please help out where i m going wrong?? What and where to put exactly to remove the exception and run the code successfully??i dont want Treenode to be used in backhand. Thanks....

+4  A: 

You're going too far in your loop.

for( int i = 0; i <= attCol.Count;i++)

should be

for( int i = 0; i < attCol.Count;i++)
Chris Hynes
Hey thanks for the answer but in listbox it is displayed String[] arrayCan u plz tell me what will be the correct code to get the result in listbox?Thanks...waitg to c ur reply..
crazy_itgal