递归加载Treelist树

public partial class Form2 : Form
{
  List<TreeNodeClass> tnclist;
  public Form2()
  {
    InitializeComponent();
    LoadXml();
  }

  void GetChildNode(XmlNodeList nodeList, List<TreeNodeClass> treeList, System.Guid parentID)
  {
    System.Guid curparentID = new Guid();
    foreach (XmlNode childNode in nodeList)
    {
      switch ((childNode).Name)
      {
        case "Neutral":
              TreeNodeClass treeNode = new TreeNodeClass();
                 treeNode.ID = System.Guid.NewGuid();
              treeNode.ParentID = parentID;
              treeNode.NodeName = childNode.SelectSingleNode("Actor.Name").Attributes[0].Value;
              treeList.Add(treeNode);
              curparentID = treeNode.ID;
              break;
         case "CLitAssyGroup":
              GetChildNode(childNode.ChildNodes, treeList, curparentID);
              break;
        case "CLitActorMesh":
              GetChildNode(childNode.ChildNodes, treeList, curparentID);
              break;
        default:
              break;
      }
    }
  }

  void LoadXml()
  {
    tnclist = new List<TreeNodeClass>();
    XmlDocument doc = new XmlDocument();
    doc.Load(System.Environment.CurrentDirectory + @"\SGS-SD.smgXml");
    XmlNodeList root = doc.SelectNodes("//Server[@Type=\"CLitServerModifiable\"]/CLitAssyRoot/Neutral/Actor.Name");
    TreeNodeClass treeNode = new TreeNodeClass();
    treeNode.ID = System.Guid.NewGuid();
    treeNode.ParentID = System.Guid.NewGuid();
    treeNode.NodeName = root[0].Attributes["Value"].Value;
    tnclist.Add(treeNode);
    XmlNodeList nodelist = doc.SelectNodes("//Server[@Type=\"CLitServerModifiable\"]/CLitAssyRoot/CLitAssyGroup");
    System.Guid curparentID = treeNode.ID;
    foreach (XmlNode node in nodelist)
    {
      GetChildNode(node.ChildNodes, tnclist, curparentID);
    }
    treeList2.DataSource = tnclist;
  }

}

 

public class TreeNodeClass
{

  public System.Guid ID { get; set; }
  public System.Guid ParentID { get; set; }
  public string NodeName { get; set; }

}

posted @ 2014-04-18 13:10  帅刺猬之家  阅读(611)  评论(0编辑  收藏  举报