tags:

views:

239

answers:

3

Duplicate of how can i store the attributes of an xml node in an array of strings, you should edit the original question.

I am a newbie so I am reframing earlier asked question. I have a project where my xmldoc is created with help of XmlDocument class and then loaded with the help of xmldoc.Load() method.With the help of addnodes() method,i add nodes to tree,thus its displayed in 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.How to do it?

+1  A: 

This could use more detail, but I'll hazard a guess:

You save an XmlElement in the Tag property of the TreeNode, right? Then you want to take that XmlElement from the Tag on the AfterSelect event, and pass it to a method that will return an IEnumerable of the attributes (and values?). IEnumerable instead of string[] to be flexible.

That's pretty simple, and I'll leave the details "as an exercise", but just loop through element.Attributes, and add to a list each attribute name and value. Format them as "name=value" if you like. Then, just return the list.

John Saunders
i did exactly what you said. now i am trying to implement another method by using string array. i am not much aware of string array manipulation. please help. can you give me the syntax of how to add attributes to an array of string?
Don't use an array. Use a List<string> (List(Of String) in VB). If necessary, you can then return list.ToArray().
John Saunders
+1  A: 

I've found through bitter experience that using the Tag property builds fragile solutions. A type-safe way of mapping TreeNodes to XmlElements is to create a Dictionary<TreeNode, XmlElement>. It's a mite harder at first than using the Tag property, but it makes the association extremely explicit. (I just yesterday fixed a bug that came from one piece of code saving keys to objects in a Tag property while another one was expecting the Tag to contain the objects themselves.)

So the code in the AfterSelect event would look something like:

TreeNode n = sender as TreeNode;
if (n != null)
{
   string[] result = TreeNodeMap[n].Attributes
      .Select(x => x.Name + "=" + x.Value)
      .ToArray();
}
Robert Rossney
A: 

But TreeNode class cannote be put in backend. It can only come in front end. The above solution would give an error if i put it in backend class, I have made something like this

"public List selectedNode(XmlNode eventNode) { if (eventNode == null) return; XmlAttributeCollection attCol = eventNode.Attributes; List ab = new List(); MessageBox.Show(attCol.Count.ToString()); if (attCol != null) for (int i = 0; i <= attCol.Count; i++) { ab[i] = "Attribute name:" + attCol[i].Name + "," + "Attribute value:" + attCol[i].Value; } return ab; } " I am calling this method in front end, But i am getting an error Index out of range. Please help me rectify it. Its in relation to my main question.