Frage

hi guys i have a problem with the treeview control in asp.net. I want to fill that with a xml file but the treeview in asp.net is diffrent to the window form treeview control :/

The name of my treeview is treeview...i don't have the xml in a folder I create a xml string and use this but in this example i only want test how i can fill this treeview out of code. how i can do this with my method?

here is my code:

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);
                    }
                }
            }

my xml example:

<?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>
War es hilfreich?

Lösung

You can use XmlDataSource as Jeremy suggested or change code as below

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);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top