|
-
Apr 17th, 2009, 06:12 AM
#1
Thread Starter
Fanatic Member
[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 
-
Apr 17th, 2009, 06:24 AM
#2
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
-
Apr 17th, 2009, 06:46 AM
#3
Thread Starter
Fanatic Member
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 
-
Apr 17th, 2009, 06:47 AM
#4
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
-
Apr 17th, 2009, 07:03 AM
#5
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:

Hope that helps!!
Gary
-
Apr 17th, 2009, 07:08 AM
#6
Thread Starter
Fanatic Member
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 
-
Apr 17th, 2009, 07:13 AM
#7
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
-
Apr 17th, 2009, 07:42 AM
#8
Thread Starter
Fanatic Member
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 
-
Apr 17th, 2009, 07:44 AM
#9
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
-
Apr 17th, 2009, 08:00 AM
#10
Thread Starter
Fanatic Member
Re: Import XML file directly into Treeview
Cheers for your help dude
 Life is one big rock tune 
-
May 20th, 2009, 08:40 AM
#11
Thread Starter
Fanatic Member
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 
-
May 20th, 2009, 09:04 AM
#12
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:

Hope that helps!!
Gary
-
May 20th, 2009, 09:39 AM
#13
Thread Starter
Fanatic Member
Re: Import XML file directly into Treeview
Thanks Gary I'll give that a go
 Life is one big rock tune 
-
May 20th, 2009, 09:54 AM
#14
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
-
May 27th, 2009, 08:19 AM
#15
Thread Starter
Fanatic Member
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 
-
May 27th, 2009, 08:20 AM
#16
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
-
May 27th, 2009, 08:39 AM
#17
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|