471,338 Members | 1,009 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,338 software developers and data experts.

TreeView datasource ?

dear all,

Is there an easy way to bind a treeview control with an XML object as
datasource?
In a similar way as the dataset is doing, and build columns accroding to
XML node, I could imagine a treeview which build its notes accordind to XML
object too

how can I do that?

thanks for wour help
serge
Jul 21 '05 #1
2 7859
serge calderara wrote:
dear all,

Is there an easy way to bind a treeview control with an XML object as
datasource?
In a similar way as the dataset is doing, and build columns accroding to
XML node, I could imagine a treeview which build its notes accordind to XML
object too

how can I do that?

thanks for wour help
serge


Funny you should ask, becase I am working on a project where i do just
that. I am retrieving XML data from a web service and I use that to
populate a tree control. My XML data is highly tailored to the
application, which is why you see special parsing in the AddNode method.

private void populateBox()
{
//Object[] objarray = lc.AllLocations();

lcx = lc.LocationsTopLevel();

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

//dom.AppendChild(lcx);

//dom.Load(textBox1.Text);
dom.AppendChild(dom.ImportNode(
lcx
,true));

//inXmlNode=inXmlNode.SelectSingleNode("//position[@row='1']");

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

// SECTION 3. 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.

try
{

if (inXmlNode.HasChildNodes)
{
nodeList = inXmlNode.ChildNodes;
for(i = 0; i<=nodeList.Count - 1; i++)
{
xNode = inXmlNode.ChildNodes[i];

if(xNode.Name=="position")
inTreeNode.Nodes.Add(new TreeNode(
xNode.Attributes.GetNamedItem("row").InnerText.ToS tring() + " " +
xNode.Attributes.GetNamedItem("bay").InnerText.ToS tring() + " " +
xNode.Attributes.GetNamedItem("level").InnerText.T oString()
));
if(xNode.Name=="client")
inTreeNode.Nodes.Add(new TreeNode(
xNode.Attributes.GetNamedItem("id").InnerText.ToSt ring()));

if(xNode.Name=="partnum")
inTreeNode.Nodes.Add(new TreeNode(
xNode.InnerText));


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();
//inTreeNode.Text = (inXmlNode.InnerText).Trim();
if(inXmlNode.Name=="position")
inTreeNode.Nodes.Add(new TreeNode(
inXmlNode.Attributes.GetNamedItem("row").InnerText .ToString() + " " +
inXmlNode.Attributes.GetNamedItem("bay").InnerText .ToString() + " " +
inXmlNode.Attributes.GetNamedItem("level").InnerTe xt.ToString()
));

}

}
catch (Exception e)
{
Debug.WriteLine(e.ToString());
}
}
Jul 21 '05 #2
I have a crystal ball...
Its is strange that XML is a hierarchical structure of any type of data an
nothing is existing on just placeing an XML source as a datasource for a
treeview, as treeview is really matching that XMl node.

Have you seen or heard something in 2005 beta 2 on that topic ?

"ja*****@texeme.com" wrote:
serge calderara wrote:
dear all,

Is there an easy way to bind a treeview control with an XML object as
datasource?
In a similar way as the dataset is doing, and build columns accroding to
XML node, I could imagine a treeview which build its notes accordind to XML
object too

how can I do that?

thanks for wour help
serge


Funny you should ask, becase I am working on a project where i do just
that. I am retrieving XML data from a web service and I use that to
populate a tree control. My XML data is highly tailored to the
application, which is why you see special parsing in the AddNode method.

private void populateBox()
{
//Object[] objarray = lc.AllLocations();

lcx = lc.LocationsTopLevel();

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

//dom.AppendChild(lcx);

//dom.Load(textBox1.Text);
dom.AppendChild(dom.ImportNode(
lcx
,true));

//inXmlNode=inXmlNode.SelectSingleNode("//position[@row='1']");

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

// SECTION 3. 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.

try
{

if (inXmlNode.HasChildNodes)
{
nodeList = inXmlNode.ChildNodes;
for(i = 0; i<=nodeList.Count - 1; i++)
{
xNode = inXmlNode.ChildNodes[i];

if(xNode.Name=="position")
inTreeNode.Nodes.Add(new TreeNode(
xNode.Attributes.GetNamedItem("row").InnerText.ToS tring() + " " +
xNode.Attributes.GetNamedItem("bay").InnerText.ToS tring() + " " +
xNode.Attributes.GetNamedItem("level").InnerText.T oString()
));
if(xNode.Name=="client")
inTreeNode.Nodes.Add(new TreeNode(
xNode.Attributes.GetNamedItem("id").InnerText.ToSt ring()));

if(xNode.Name=="partnum")
inTreeNode.Nodes.Add(new TreeNode(
xNode.InnerText));


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();
//inTreeNode.Text = (inXmlNode.InnerText).Trim();
if(inXmlNode.Name=="position")
inTreeNode.Nodes.Add(new TreeNode(
inXmlNode.Attributes.GetNamedItem("row").InnerText .ToString() + " " +
inXmlNode.Attributes.GetNamedItem("bay").InnerText .ToString() + " " +
inXmlNode.Attributes.GetNamedItem("level").InnerTe xt.ToString()
));

}

}
catch (Exception e)
{
Debug.WriteLine(e.ToString());
}
}

Jul 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Craig | last post: by
2 posts views Thread by Ryo | last post: by
2 posts views Thread by serge calderara | last post: by
reply views Thread by Michael Lewis | last post: by
26 posts views Thread by JJ | last post: by
3 posts views Thread by =?Utf-8?B?TGVzbGll?= | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.