how to represent xml document on treeviewer using c#

I am student and i am biggener user of c#
I want to represent xml document on treeviewer
can any personne help me
thanks




Answer this question

how to represent xml document on treeviewer using c#

  • JoeDeVirs

    i essaye this code but the element are decaled
    if (inXmlNode.Attributes != null)
    {


    foreach (XmlAttribute attribute in inXmlNode.Attributes)
    {
    inTreeNode.Nodes.Add(new TreeNode(attribute.Name));


    }



  • Nissan

    the code show only the element of the document xml but the attribut don't appear in the treeviewer
    I need to add foreach element his attribut to the tree
    i give you an example:
    <employes>
    <liste nom=" la liste des travailleurs">
    </employes>

    in this example the attribute "nom" and his valeur don't appear in the
    TreeViewer
    dou you understand my problem now
    tkinks



  • Gerd Sauermann

    Sorry. I don't understand last sentence.

    If this is the question, can you rephrase it.

    If your initial issue was resolved, but you have new (even related) questions, sometime it helps to start new thread and explain them from blank sheet.

    Thanks

  • TBone

    As a quote from a MSDN Article:


    try
    {
    // Create a DOM Document and load the XML data into it.
    XmlDocument dom = new XmlDocument();
    dom.Load(textBox1.Text);

    // Initialize the TreeView control.
    treeView1.Nodes.Clear();
    treeView1.Nodes.Add(new TreeNode(dom.DocumentElement.Name));
    TreeNode tNode = new TreeNode();
    tNode = treeView1.Nodes[ 0 ];

    // Populate the TreeView with the DOM nodes.
    AddNode(dom.DocumentElement, tNode);
    treeView1.ExpandAll();
    }
    catch(XmlException xmlEx)
    {
    MessageBox.Show(xmlEx.Message);
    }
    catch(Exception ex)
    {
    MessageBox.Show(ex.Message);
    }




    private void AddNode(XmlNode inXmlNode, TreeNode inTreeNode)
    {
    XmlNode xNode;
    TreeNode tNode;
    XmlNodeList nodeList;
    int i;

    // Loop through the XML nodes until the leaf is reached.
    // Add the nodes to the TreeView during the looping process.
    if (inXmlNode.HasChildNodes)
    {
    nodeList = inXmlNode.ChildNodes;
    for(i = 0; i<=nodeList.Count - 1; i++)
    {
    xNode = inXmlNode.ChildNodes[ i ];
    inTreeNode.Nodes.Add(new TreeNode(xNode.Name));
    tNode = inTreeNode.Nodes[ i ];
    AddNode(xNode, tNode);
    }
    }
    else
    {
    // Here you need to pull the data from the XmlNode based on the
    // type of node, whether attribute values are required, and so forth.
    inTreeNode.Text = (inXmlNode.OuterXml).Trim();
    }
    }




  • Schmidi

    And when you use something like this


    // Create a DOM Document and load the XML data into it.
    XmlDocument dom = new XmlDocument();
    dom.Load(textBox1.Text);

    // Initialize the TreeView control.
    treeView1.Nodes.Clear();
    treeView1.Nodes.Add(new TreeNode(dom.DocumentElement.Name));
    TreeNode tNode = new TreeNode();
    tNode = treeView1.Nodes[ 0 ];

    foreach(XmlNode innerNode in dom.DocumentElement.ChildNodes)
    {
    if(innerNode.NodeType == XmlNodeType.Element && innerNode.Name.Equals( "employes" ))
    {
    tNode.Nodes.Add( innerNode.Attributes["nom"].Value );
    }
    }

    // Populate the TreeView with the DOM nodes.
    AddNode(dom.DocumentElement, tNode);
    treeView1.ExpandAll();




  • Kelvin Penus

    my goal is torepresente any xml document ( elements and attributs) in TreeViewer
    can any person help me


  • how to represent xml document on treeviewer using c#