Results 1 to 17 of 17

Thread: [RESOLVED] Import XML file directly into Treeview

  1. #1

    Thread Starter
    Fanatic Member Valleysboy1978's Avatar
    Join Date
    Nov 2004
    Location
    Planet Xeoroaniar CC Posts:1,928,453,459,361
    Posts
    770

    [RESOLVED] Import XML file directly into Treeview

    Hey all

    Okay so here, as the bard would say, is the rub.
    I need to import an XML file into a treeview. Simple enough but the problem is that if you use objects such as XMLDocument you lose a lot of the XML file (such as the "<" or ">").
    What I essentially need is every tag to be input into the treeview as it is in the XML file so that the tree view will be an identical visual representation of the XML file

    Thanks
    Last edited by Valleysboy1978; May 27th, 2009 at 08:38 AM. Reason: add resolved tag
    Life is one big rock tune

  2. #2
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Import XML file directly into Treeview

    Hey,

    Is it possible that you can show can example, both in code, and via a screen shot, of what you are currently doing, and how the end result it not what you are expecting?

    It is difficult to currently visualise the problem based on what you have described.

    Thanks

    Gary

  3. #3

    Thread Starter
    Fanatic Member Valleysboy1978's Avatar
    Join Date
    Nov 2004
    Location
    Planet Xeoroaniar CC Posts:1,928,453,459,361
    Posts
    770

    Re: Import XML file directly into Treeview

    Hiya

    Essentially the XML file would look like so:

    HTML Code:
    <Family>
       <Parent>
          Name="John Doe"
          <Child>
              Name="Jane Doe"
          </Child>
       </Parent>
    </Family>
    the tree view would look identical to that.
    Life is one big rock tune

  4. #4
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Import XML file directly into Treeview

    Hey,

    So what have you already tried that didn't work?

    As far as I am aware, there is not way to directly link an XML File to a treeview, you have to manually walk the XML File and load the corresponding nodes into the treeview.

    Gary

  5. #5
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Import XML file directly into Treeview

    Hey,

    One way to do this would be to use code similar to the following:

    Code:
    private void button1_Click(object sender, EventArgs e)
            {
                treeView1.Nodes.Clear();
    
                XmlReader reader;
                openFileDialog1.ShowDialog();
                reader = XmlReader.Create(openFileDialog1.FileName);
                TreeNode aNode = new TreeNode();
                TreeNode bNode;
                TreeNode parentNode = new TreeNode();
    
                while(reader.Read())
                {
                    switch (reader.NodeType)
                    {
                        case XmlNodeType.Element:
                            aNode = new TreeNode(reader.Name);
                            if (reader.AttributeCount > 0)
                            {
                                reader.MoveToFirstAttribute();
                                aNode.Text += " " + reader.Name + "=" + reader.Value;
                                for (int i = 1; i < reader.AttributeCount; i++)
                                {
                                    reader.MoveToNextAttribute();
                                    aNode.Text += " " + reader.Name + "=" + reader.Value;
                                }
                            }
                            if (parentNode.Text == "")
                            {
                                treeView1.Nodes.Add(aNode);
                            }
                            else
                            {
                                parentNode.Nodes.Add(aNode);
                            }
                            parentNode = aNode;
                            break;
                        case XmlNodeType.EndElement:
                            parentNode = parentNode.Parent;
                            break;
                        case XmlNodeType.Text:
                            bNode = new TreeNode(reader.Value);
                            aNode.Nodes.Add(bNode);
                            break;
                    }
                }
            }
    This code gives the following:

    Name:  Read XML.jpg
Views: 1184
Size:  15.7 KB

    Hope that helps!!

    Gary

  6. #6

    Thread Starter
    Fanatic Member Valleysboy1978's Avatar
    Join Date
    Nov 2004
    Location
    Planet Xeoroaniar CC Posts:1,928,453,459,361
    Posts
    770

    Re: Import XML file directly into Treeview

    Hi Gary

    Thanks. I've already tried that but as you can see none of the tags ("<") or the end tags are included. This is what I've got so far but I can't figure out how to get the end tags in place:
    Code:
           private void button1_Click(object sender, EventArgs e)
            {
                try
                {
                    XmlDocument dom = new XmlDocument();
                    dom.Load(textBox1.Text);
    
                    treeView1.Nodes.Clear();
                    treeView1.Nodes.Add(new TreeNode("<" + dom.DocumentElement.Name + ">"));
                    TreeNode tNode = new TreeNode();
                    tNode = treeView1.Nodes[0];
    
                    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;
             string nodeName = "";
             int i;
    
              if (inXmlNode.HasChildNodes)
             {
                nodeList = inXmlNode.ChildNodes;
                for(i = 0; i<=nodeList.Count - 1; i++)
                {
                   xNode = inXmlNode.ChildNodes[i];
                   if (xNode.NodeType == XmlNodeType.Element)
                   {
                       inTreeNode.Nodes.Add(new TreeNode("<" +xNode.Name + ">"));
                       nodeName = xNode.Name;
                   }
                   else if (xNode.NodeType == XmlNodeType.Text)
                   {
                       inTreeNode.Nodes.Add(new TreeNode(xNode.Name));
                   }
                   tNode = inTreeNode.Nodes[i];
                   AddNode(xNode, tNode);
                   }
                }
             else
             {
                inTreeNode.Text = (inXmlNode.OuterXml).Trim();
             }
          }
    Life is one big rock tune

  7. #7
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Import XML file directly into Treeview

    Hey,

    Ah, I see what you are after now...

    My question would be though, why do you need them?

    To help answer your question though, you should be able to do something using the XmlNodeType.EndElement to try and figure out when you should use the closing brackets.

    In my opinion, this isn't needed though, as the existence of the Tree Node indicates the start and the stop of the node, and which nodes are contained within it.

    What exactly are you trying to achieve?

    Gary

  8. #8

    Thread Starter
    Fanatic Member Valleysboy1978's Avatar
    Join Date
    Nov 2004
    Location
    Planet Xeoroaniar CC Posts:1,928,453,459,361
    Posts
    770

    Re: Import XML file directly into Treeview

    The client wants to see the XML file in the treeview as it will be written. Don't ask, they're French
    Life is one big rock tune

  9. #9
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Import XML file directly into Treeview

    Hey,

    Ah, ok, it's a client requirement.

    I would look into using the EndElement to see if that will solve your problem, or as another solution, you could just use the OuterXml property of the XmlDocument and display that in TextBox or something similar.

    Just a thought.

    Gary

  10. #10

    Thread Starter
    Fanatic Member Valleysboy1978's Avatar
    Join Date
    Nov 2004
    Location
    Planet Xeoroaniar CC Posts:1,928,453,459,361
    Posts
    770

    Re: Import XML file directly into Treeview

    Cheers for your help dude
    Life is one big rock tune

  11. #11

    Thread Starter
    Fanatic Member Valleysboy1978's Avatar
    Join Date
    Nov 2004
    Location
    Planet Xeoroaniar CC Posts:1,928,453,459,361
    Posts
    770

    Re: Import XML file directly into Treeview

    Okay still struggling over this.
    I just cannot get the XML file into a tree view so that it is an EXACT representation of the XML file's structure.

    Any ideas would be welcome.
    Have already tried the M$ support version and it was hopeless.

    Thanks
    VB
    Life is one big rock tune

  12. #12
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Import XML file directly into Treeview

    Hey,

    I this what you are after:

    Code:
    private void button1_Click(object sender, EventArgs e)
            {
                treeView1.Nodes.Clear();
    
                XmlReader reader;
                //openFileDialog1.ShowDialog();
                reader = XmlReader.Create("XMLFile1.xml");
                TreeNode aNode = new TreeNode();
                TreeNode bNode;
                TreeNode endNode;
                TreeNode parentNode = new TreeNode();
    
                while(reader.Read())
                {
                    switch (reader.NodeType)
                    {
                        case XmlNodeType.Element:
                            aNode = new TreeNode("<" + reader.Name + ">");
                            if (reader.AttributeCount > 0)
                            {
                                reader.MoveToFirstAttribute();
                                aNode.Text += " " + reader.Name + "=" + reader.Value;
                                for (int i = 1; i < reader.AttributeCount; i++)
                                {
                                    reader.MoveToNextAttribute();
                                    aNode.Text += " " + reader.Name + "=" + reader.Value;
                                }
                            }
                            if (parentNode.Text == "")
                            {
                                treeView1.Nodes.Add(aNode);
                            }
                            else
                            {
                                parentNode.Nodes.Add(aNode);
                            }
                            parentNode = aNode;
                            break;
                        case XmlNodeType.EndElement:
                            endNode = new TreeNode("</" + reader.Name + ">");
                            parentNode = parentNode.Parent;
                            if (parentNode == null)
                            {
                                treeView1.Nodes.Add(endNode);
                            }
                            else
                            {
                                parentNode.Nodes.Add(endNode);
                            } 
                            
                            break;
                        case XmlNodeType.Text:
                            bNode = new TreeNode(reader.Value);
                            aNode.Nodes.Add(bNode);
                            break;
                    }
                }
            }
    Which results in:

    Name:  Form1.jpg
Views: 1032
Size:  19.3 KB

    Hope that helps!!

    Gary

  13. #13

    Thread Starter
    Fanatic Member Valleysboy1978's Avatar
    Join Date
    Nov 2004
    Location
    Planet Xeoroaniar CC Posts:1,928,453,459,361
    Posts
    770

    Re: Import XML file directly into Treeview

    Thanks Gary I'll give that a go
    Life is one big rock tune

  14. #14
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Import XML file directly into Treeview

    Okay, let me know how you get on.

    The example used a very simple XML file, and may not work for a more complex example.

    Gary

  15. #15

    Thread Starter
    Fanatic Member Valleysboy1978's Avatar
    Join Date
    Nov 2004
    Location
    Planet Xeoroaniar CC Posts:1,928,453,459,361
    Posts
    770

    Re: Import XML file directly into Treeview

    Thanks again Gary, tried a more complex XML file and it seemed fine
    Life is one big rock tune

  16. #16
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Import XML file directly into Treeview

    Sweet!! I was expecting it to bomb out

    Glad to hear it works. Is it what your client is after?

    Gary

  17. #17

    Thread Starter
    Fanatic Member Valleysboy1978's Avatar
    Join Date
    Nov 2004
    Location
    Planet Xeoroaniar CC Posts:1,928,453,459,361
    Posts
    770

    Re: [RESOLVED] Import XML file directly into Treeview

    Apologies, forgot to add "[RESOLVED]" tag. Now in place
    Life is one big rock tune

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width