[RESOLVED] Import XML file directly into Treeview
Hey all :wave:
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
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
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.
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
1 Attachment(s)
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:
Attachment 70511
Hope that helps!!
Gary
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();
}
}
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
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
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
Re: Import XML file directly into Treeview
Cheers for your help dude
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
:sick:
1 Attachment(s)
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:
Attachment 71097
Hope that helps!!
Gary
Re: Import XML file directly into Treeview
Thanks Gary I'll give that a go
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
Re: Import XML file directly into Treeview
Thanks again Gary, tried a more complex XML file and it seemed fine
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
Re: [RESOLVED] Import XML file directly into Treeview
Apologies, forgot to add "[RESOLVED]" tag. Now in place