473,387 Members | 1,606 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 8014
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Craig | last post by:
Is there a way to "tie" or "bind" an XmlDocument to a treeView, so the changes in the treeView can automatically be reflected in the XmlDocument object? (Like DataSource for a ListBox) If not,...
2
by: Ryo | last post by:
Hello ! I have an XmlDocument in memory and I want to put this document as datasource of my treeview. How to do this ? Thanks.
0
by: Ryo | last post by:
Hello ! I have this simple aspx page: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="test_treeview.aspx.cs" Inherits="test_treeview" Title="Treeview" %> <asp:Content...
2
by: serge calderara | last post by:
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...
0
by: Michael Lewis | last post by:
Does anyone know if it is possible to bind a gridview to a treeview. Ie. I want to be able to expand a treeview node and see a separate gridview for each node. The gridview will be filtered by a...
5
by: Paul | last post by:
Hi, I am a self taught VBA programmer, and I'm trying to learn VB2005 Express (The price was right). I like the look of the treeview control, and I'd like to use it as a menu system for my users,...
26
by: JJ | last post by:
Is there any way you can expand a parent node on the treeview control _without a postback_ by clicking on the node text (NOT clicking the expand image URL) ? I want to format the treeview node...
3
by: =?Utf-8?B?TGVzbGll?= | last post by:
Using Visual Studio 2005 SP1 I am attempting to dynamically load a treeview control. I create an XmlDataSource and then load the data source using XmlDataSource.Data. I Load my XML string into...
0
by: Dennis Francek | last post by:
Hello I have populated a treeview from my dataset and by clicking in the treeview i want to get the corresponding table shown in a datagridview. Ive managed this by looking at each DataRow and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.