比如我有这样的一个xml文件:
<calculationArc from="newItem2" to="newItem3"/>
<calculationArc from="newItem5" to="newItem6"/>
<calculationArc from="newItem5" to="newItem7"/>
<calculationArc from="newItem2" to="newItem5"/>
<calculationArc from="newItem1" to="newItem2"/>
<calculationArc from="newItem1" to="newItem4"/>
<calculationArc from="newItem0" to="newItem1"/>
它描述了一个树形结构,from代表是to的父节点,这样使他在treeview中显示他们的树形关系,xml文件不一定就是以上例子这么简单,可能更复杂,还请哪位高手请教??
<calculationArc from="newItem2" to="newItem3"/>
<calculationArc from="newItem5" to="newItem6"/>
<calculationArc from="newItem5" to="newItem7"/>
<calculationArc from="newItem2" to="newItem5"/>
<calculationArc from="newItem1" to="newItem2"/>
<calculationArc from="newItem1" to="newItem4"/>
<calculationArc from="newItem0" to="newItem1"/>
它描述了一个树形结构,from代表是to的父节点,这样使他在treeview中显示他们的树形关系,xml文件不一定就是以上例子这么简单,可能更复杂,还请哪位高手请教??
http://www.cnblogs.com/wts/archive/2007/03/26/688760.html
可能对你有点帮助
然后将DataTable按照From排序
排序后数据这样
from to
0
1
1
2
2
5
5
下面方法是绑定到treeview
你可以修改下
//自定义方法,给treeview榜定数据
/// <summary>
/// BindTree给Treeview绑定数据
/// </summary>
/// <param name="tv">TreeView</param>
/// <param name="ds">DataSet</param>
private void BindTree(TreeView tv, DataSet ds)
{
//得到数据条数
int cnt = ds.Tables[0].Rows.Count;
DataTable dt = ds.Tables[0];
//模块名称
string ModuleName = "";
//上条记录的模块名称
string PreModuleName = ""; //第一层节点
TreeNode FirstNode = new TreeNode(); //遍历所有记录,给各节点赋值
for (int index = 0; index < cnt; index++)
{
//模块节点的名称
ModuleName = dt.Rows[index]["model_name"].ToString();
//叶节点,并实例化
TreeNode PageNode = new TreeNode();
PageNode.Text = dt.Rows[index]["url_name"].ToString();
PageNode.Value = dt.Rows[index]["url_id"].ToString();
PageNode.NavigateUrl = dt.Rows[index]["url"].ToString();
PageNode.Target = dt.Rows[index]["target"].ToString(); //判断是否已经有过该节点,如无,添加新节点
if (ModuleName != PreModuleName)
{
TreeNode ModuleNode = new TreeNode();
ModuleNode.Target = "";
ModuleNode.Text = ModuleName;
tv.Nodes.Add(ModuleNode);
FirstNode = ModuleNode;
FirstNode.SelectAction = TreeNodeSelectAction.None;
} FirstNode.ChildNodes.Add(PageNode); PreModuleName = ModuleName;
}
}
http://www.cnblogs.com/godwar/archive/2008/07/05/1236291.html
“现将数据读取到DataTable中去
然后将DataTable按照From排序
排序后数据这样
from to
0
1
1
2
2
5
5
”你这个是按照什么排序的呢,这个也是我所遇到的难点??
class nodeclass
{
public nodeclass ParentNode{get;set;}
public IList<nodeclass> SubNodes{get;set;}
public object data{get;set;}
}读取xml文件,并生成当前结点的实例,找到其父结点实例,添加到SubNodes列表中,这样在内存中的数据是一个树形结构了,然后你可以选择递归或迭代显示到treeview上去;事情到这一步我想就很简单了;
Default.aspx.cs文件:
using System.Xml;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("data.xml"));
XmlNode node = doc.DocumentElement;
InitTree ( this.TreeView1.Nodes, node );
} private void InitTree ( TreeNodeCollection nodes, XmlNode node )
{
XmlNodeList nodeList = node.ChildNodes;
foreach ( XmlNode subNode in nodeList )
{
TreeNode tmpNode = new TreeNode ();
tmpNode.Text = ((XmlElement)subNode).GetAttribute("name");
tmpNode.NavigateUrl = ((XmlElement)subNode).GetAttribute("url");
nodes.Add ( tmpNode );
InitTree ( tmpNode.ChildNodes, subNode );
}
}
}data.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<root>
<baidu name="Baidu" url="http://www.baidu.com">
<hao name="Hao" url="http://www.hao123.com">
<wangyi name="Wangyi" url="http://www.163.com">
</wangyi>
</hao>
<sina name="Sina" url="http://www.sina.com.cn">
<qq name="QQ" url="http://www.qq.com">
</qq>
</sina>
</baidu>
</root>