大家好我在asp.net中遇到了treeview控件的问题。我想用xml文件填充它,但asp.net中的树视图与窗体树视图控件不同:/
我的树视图的名称是树视图...我没有文件夹中的xml我创建一个xml字符串并使用它但在这个例子中我只想测试如何从代码中填充这个树视图。我怎么用我的方法做到这一点?
这是我的代码:
XElement doc = XElement.Load("~/App_Data/test_xml.xml");
TreeNode Feature;
TreeNode User;
foreach (XElement state in doc.Descendants("FEATURE"))
{
Feature = treeview.Nodes.Add(state.Attribute("NAME").Value);
foreach (XElement region in state.Descendants("USER"))
{
User =
Feature.Nodes.Add(region.Attribute("NAME").Value);
foreach (XElement area in region.Descendants("NAME"))
{
User.Nodes.Add(area.Attribute("NAME").Value);
}
}
}
我的xml示例:
<?xml version="1.0" encoding="UTF-8"?>
<LM-X STAT_VERSION="3.32">
<LICENSE_PATH TYPE="NETWORK" HOST="Server002" SERVER_VERSION="4.4.4" UPTIME="53 day(s) 21 hour(s) 10 min(s) 50 sec(s)">
<FEATURE NAME="GlobalZoneEU" VERSION="12.0" VENDOR="ALTAIR" START="2013-03-26" END="2014-03-31" USED_LICENSES="111720" TOTAL_LICENSES="147000" SHARE="CUSTOM ,VIRTUAL">
<USER NAME="SYSTEM" HOST="Server1" IP="" USED_LICENSES="2000" LOGIN_TIME="2013-04-17 12:42" CHECKOUT_TIME="2013-04-17 12:42" SHARE_CUSTOM=""/>
>
<USER NAME="Admin" HOST="Server1" IP="" USED_LICENSES="720" LOGIN_TIME="2013-04-17 12:44" CHECKOUT_TIME="2013-04-17 12:44" SHARE_CUSTOM=""/>
>
<USER NAME="Test.A" HOST="4327" IP="" USED_LICENSES="21000" LOGIN_TIME="2013-05-21 07:52" CHECKOUT_TIME="2013-05-21 07:52" SHARE_CUSTOM=""/>
>
<USER NAME="Test.B" HOST="4327" IP="" USED_LICENSES="6000" LOGIN_TIME="2013-05-21 07:54" CHECKOUT_TIME="2013-05-21 07:54" SHARE_CUSTOM=""/>
>
<USER NAME="Test.C" HOST="4970" IP="" USED_LICENSES="21000" LOGIN_TIME="2013-05-21 08:15" CHECKOUT_TIME="2013-05-21 08:15" SHARE_CUSTOM=""/>
</FEATURE>
<FEATURE NAME="HWAIFPBS" VERSION="12.0" VENDOR="ALTAIR" START="2013-03-26" END="2014-03-31" USED_LICENSES="0" TOTAL_LICENSES="2147483647" SHARE="CUSTOM ,VIRTUAL"/>
<FEATURE NAME="HWAWPF" VERSION="12.0" VENDOR="ALTAIR" START="2013-03-26" END="2014-03-31" USED_LICENSES="0" TOTAL_LICENSES="2147483647" SHARE="CUSTOM ,VIRTUAL"/>
<FEATURE NAME="HWAcuconsole" VERSION="12.0" VENDOR="ALTAIR" START="2013-03-26" END="2014-03-31" USED_LICENSES="0" TOTAL_LICENSES="2147483647" SHARE="CUSTOM ,VIRTUAL"/>
</LICENSE_PATH>
</LM-X>
答案 0 :(得分:1)
您可以使用XmlDataSource
作为Jeremy建议或更改代码,如下所示
XElement doc = XElement.Load("~/App_Data/test_xml.xml");
TreeNode root = new TreeNode("FEATURES");
foreach (XElement state in doc.Descendants("FEATURE"))
{
TreeNode feature = new TreeNode(state.Attribute("NAME").Value);
foreach (XElement region in state.Descendants("USER"))
{
TreeNode user = new TreeNode(region.Attribute("NAME").Value);
foreach (XElement area in region.Descendants("NAME"))
{
user.ChildNodes.Add(new TreeNode(area.Attribute("NAME").Value));
}
feature.ChildNodes.Add(user);
}
root.ChildNodes.Add(feature);
}
treeview.Nodes.Add(root);