Click here to Skip to main content
15,884,099 members
Home / Discussions / C#
   

C#

 
Questiondifferent Behaviour of controls according to login Pin
AlokZanwar15-Feb-09 21:01
AlokZanwar15-Feb-09 21:01 
AnswerRe: different Behaviour of controls according to login Pin
Expert Coming15-Feb-09 21:09
Expert Coming15-Feb-09 21:09 
AnswerRe: different Behaviour of controls according to login Pin
Nuri Ismail15-Feb-09 21:43
Nuri Ismail15-Feb-09 21:43 
GeneralRe: different Behaviour of controls according to login Pin
Expert Coming15-Feb-09 21:57
Expert Coming15-Feb-09 21:57 
AnswerRe: different Behaviour of controls according to login Pin
MumbleB15-Feb-09 22:08
MumbleB15-Feb-09 22:08 
AnswerRe: different Behaviour of controls according to login Pin
arun_pk15-Feb-09 23:24
arun_pk15-Feb-09 23:24 
QuestionC# :: Updating SQL Table Form DataGridView Pin
WinSolution15-Feb-09 21:00
WinSolution15-Feb-09 21:00 
QuestionHow to show a selected treeview node' s attribute & value in listbox using C#? [modified] Pin
rasingh115-Feb-09 20:57
rasingh115-Feb-09 20:57 
Hi,
i have a treeView,Load Button,listbox and textbox on my winform.
i want that wen a user loads a XML file it should be shown as TreeView on my winform.
Then wen a node is selected in the treeview its attributes should be displayed in the listbox and values in Textbox.
i have written a part of code which shows all the attributes at once in the listbox on loading the XML file.As i am new to C# i dont know what to write in AfterSelect().The code is here
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Xml;
using System.Text;
using System.Windows.Forms;

namespace LoadXMLtreeDisplay
{
    public partial class TreeDisplay : Form
    {
        //Declaring Treeview in Listbox
        TreeView listViewobj = new TreeView();

        //Declare XML Document object
        XmlDocument xdoc = new XmlDocument();
        
         //Constructor of class
        public TreeDisplay( )
        {
            InitializeComponent();
            this.Controls.Add(treeViewObj);
            this.listBoxshow.SelectedIndexChanged += new System.EventHandler(this.listBoxshow_SelectedIndexChanged);
        }//constructor

        private void treeDocLoadMethod(string nameofFile)
        {
            try
            {
               txtBoxfile.Text = nameofFile;

                xdoc.Load(nameofFile);

                this.treeViewObj.Nodes.Clear();
                this.treeViewObj.Nodes.Add(new TreeNode(xdoc.DocumentElement.Name));
                TreeNode tNodeObj = new TreeNode();
                tNodeObj = this.treeViewObj.Nodes[0];

                XmlNodeList nodeList = xdoc.SelectNodes("//settings/tables/table/name[. ='{0}']");
                XmlNode xNode = xdoc.DocumentElement;//nodeList.Item(0).ParentNode;

               ConvertAllXmlnodetoTreenode(xdoc, treeViewObj.Nodes);

                treeViewObj.Nodes[0].Expand();
                treeViewObj.CollapseAll();

                Cursor = System.Windows.Forms.Cursors.Default;
            }
        
            catch (XmlException xmlex)
           {
             MessageBox.Show(xmlex.Message, "ERROR");
            }//catch
            catch(Exception exp)
            {
                txtBoxfile.Text = exp.Message;
            }
        }//treeDocLoadMethod        
private void ConvertAllXmlnodetoTreenode(XmlNode xmlNode, TreeNodeCollection treeNodeCollection)
        {
            TreeNode treeNodeobj = treeNodeCollection.Add(xmlNode.Name);
            switch (xmlNode.NodeType)
            {
                case XmlNodeType.ProcessingInstruction:
                case XmlNodeType.XmlDeclaration:
                    treeNodeobj.Text = "";
                      break;
                case XmlNodeType.Element:
                    treeNodeobj.Text = "<" + xmlNode.Name + ">";
                    break;
                case XmlNodeType.Attribute:
                    treeNodeobj.Text = "ATTRIBUTE: " + xmlNode.Name;
                    listBoxshow.Items.Add(treeNodeobj.Text);
                    break;
                case XmlNodeType.Text:
                case XmlNodeType.CDATA:
                    treeNodeobj.Text = xmlNode.Value;
                    listBoxshow.Items.Add(treeNodeobj.Text);
                    break;
                case XmlNodeType.Comment:
                    treeNodeobj.Text = "<!--" + xmlNode.Value + "-->";
                    break;
            }//switch
            if (xmlNode.Attributes != null)
            {
                foreach (XmlAttribute attribute in xmlNode.Attributes)
                {
                    ConvertAllXmlnodetoTreenode(attribute, treeNodeobj.Nodes);
                }
            }
             foreach (XmlNode childNode in xmlNode.ChildNodes)
             {
                 ConvertAllXmlnodetoTreenode(childNode, treeNodeobj.Nodes);
             }
           }// ConvertAllXmlnodetoTreenode Method
    private void ConvertAddTreeNodestoTree(XmlNode xnode, TreeNode tnode)
         {
             XmlNode xNode;
             TreeNode treeNode;
             XmlNodeList nodeList;
      
             if (xnode.HasChildNodes)
             {
                 nodeList = xnode.ChildNodes;
                 for (int i = 0; i <= nodeList.Count-1;i++)
                 {
                     xNode = xnode.ChildNodes[i];
                     tnode.Nodes.Add(new TreeNode(xNode.Name));
                     treeNode = tnode.Nodes[i];
                     ConvertAllXmlnodetoTreenode(xNode,treeNode.Nodes);
                 }//for
             }//if 
             else
             {
                 tnode.Text = xnode.OuterXml.Trim();
             }//else
         }
         private void ExpandBtn_Click(object sender, EventArgs e)
         {
            try
             {
                 if (this.ExpandBtn.Text == "Expand TreeNodes")
                 {
                     this.treeViewObj.ExpandAll();
                     this.ExpandBtn.Text = "Collapse TreeNodes";
                  }//if
              else
                 {
                    this.treeViewObj.CollapseAll();
                    this.ExpandBtn.Text = "Expand TreeNodes";
                 }//else
             }//try
             catch(Exception exp)
             {
                 MessageBox.Show(exp.Message,"Error");
             }//catch
         }//Expand button
    private void btnBrowse_Click(object sender, EventArgs e)
         {
             txtBoxfile.Clear();
             listBoxshow.Items.Clear();
             listBoxeg.Items.Clear();
             ExpandBtn.Text = "Expand TreeNodes";
             OpenFileDialog open = new OpenFileDialog();
             //open.InitialDirectory = @"C:\";
             open.Filter = "XML Files(*.xml)|(*.xhtml)|All files(*.*)|*.*";
             open.FilterIndex = 2;
             open.RestoreDirectory = true;
             if (open.ShowDialog(this)== DialogResult.OK)
             {
                 txtBoxfile.Text = open.FileName;
                 treeDocLoadMethod(open.FileName); //this variable gives the name of selected file
             }
           }//Browse button
   private void treeViewObj_AfterSelect(object sender, TreeViewEventArgs e)
        {
            listBoxeg.Items.Clear();
            XmlNode xNode = xdoc.DocumentElement;
            TreeNode selNode = e.Node;
            textBox1.Text = selNode.Text;
                      
          if (e.Node.TreeView.SelectedNode != null)
            {
                foreach (XmlAttribute attribute in xNode.Attributes)
                {
                    ShowinListboxandTextbox(attribute, selNode.Nodes);
                    selNode.Text = "ATTRIBUTE: " + xNode.Name;
                    listBoxeg.Items.Add(selNode.Text);
                }
            }
          } 
       private void ShowinListboxandTextbox(XmlNode xmlNode,TreeNodeCollection treeNodecollect)
         {
             TreeNode treeNodeobj = treeNodecollect.Add(xmlNode.Name);
             switch (xmlNode.NodeType)
             {
                case XmlNodeType.Attribute:
                     treeNodeobj.Text = "ATTRIBUTE: " + xmlNode.Name;
                     listBoxeg.Items.Add(treeNodeobj.Text);
                     break;
                case XmlNodeType.CDATA:
                     treeNodeobj.Text = xmlNode.Value;
                     listBoxeg.Items.Add(treeNodeobj.Text);
                     break;
              }//switch              
         }//ShowinListboxandTextbox        
     }//class
}//namespace

This is not showing the desired result Frown | :( as i want if anyone can provide the correct part of code required i will be thankful. Smile | :)

modified on Monday, February 23, 2009 1:36 PM

QuestionHow to Debug C# Class Library that is invoked by an external Application? Pin
undecypherable15-Feb-09 19:45
undecypherable15-Feb-09 19:45 
AnswerRe: How to Debug C# Class Library that is invoked by an external Application? Pin
pedersen-roxen17-Feb-09 21:07
pedersen-roxen17-Feb-09 21:07 
QuestionSpeech Compare Pin
Rahul Babu15-Feb-09 19:33
Rahul Babu15-Feb-09 19:33 
AnswerRe: Speech Compare Pin
Christian Graus15-Feb-09 19:57
protectorChristian Graus15-Feb-09 19:57 
QuestionRaw Data to Image in C# Pin
Dhaval Faria15-Feb-09 19:21
Dhaval Faria15-Feb-09 19:21 
AnswerRe: Raw Data to Image in C# Pin
Guffa15-Feb-09 19:47
Guffa15-Feb-09 19:47 
QuestionHow to round the result up to the integer? Pin
Ravi Munde15-Feb-09 19:13
Ravi Munde15-Feb-09 19:13 
AnswerRe: How to round the result up to the integer? Pin
S a n d y15-Feb-09 19:34
S a n d y15-Feb-09 19:34 
AnswerRe: How to round the result up to the integer? Pin
Vikram A Punathambekar15-Feb-09 19:43
Vikram A Punathambekar15-Feb-09 19:43 
Questioncan we force the GC to collect memory . Pin
prasadbuddhika15-Feb-09 18:54
prasadbuddhika15-Feb-09 18:54 
AnswerRe: can we force the GC to collect memory . Pin
Gonxh Aniket15-Feb-09 18:57
Gonxh Aniket15-Feb-09 18:57 
GeneralRe: can we force the GC to collect memory . Pin
prasadbuddhika15-Feb-09 19:06
prasadbuddhika15-Feb-09 19:06 
GeneralRe: can we force the GC to collect memory . Pin
Vikram A Punathambekar15-Feb-09 19:47
Vikram A Punathambekar15-Feb-09 19:47 
GeneralRe: can we force the GC to collect memory . Pin
Christian Graus15-Feb-09 19:58
protectorChristian Graus15-Feb-09 19:58 
GeneralRe: can we force the GC to collect memory . Pin
Wes Aday16-Feb-09 4:09
professionalWes Aday16-Feb-09 4:09 
GeneralRe: can we force the GC to collect memory . Pin
N a v a n e e t h15-Feb-09 20:58
N a v a n e e t h15-Feb-09 20:58 
QuestionTreeview document to save existing xml file... Pin
raj23136215-Feb-09 18:47
raj23136215-Feb-09 18:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.