Traverse xml into dropdown list using C#

This code is for iterating the xml all the way down, to insert it into a dropdown list, with the level indent. Very basic, just the core functionality.

A couple of methods needed. Some stuff are not included, like fetching the XML.

Xml looks like:
<?xml version="1.0" encoding="utf-8" ?>
<data>
  <process name="Process_1">
    <processPart name="Part11" />
    <processPart name="part12">
      <workflow name="WorkFlowz111" />
    </processPart>
  </process>
  <process name="Process_2">
      <processPart name="Part21" />
      <processPart name="part22">        
        <workflow name="WorkFlowz221" />
      </processPart>    
  </process>
</data>

Methods:

 public ListItemCollection GetListFromXML()  
 {  
   int level = 0;  
   ListItemCollection lic = new ListItemCollection();  
   XmlDocument xDoc = new XmlDocument();  
   xDoc.LoadXml(ep.GetProcessStructureXML());  
   lic = GetListFromSiblings(xDoc.ChildNodes[1], lic, level);        
   return lic;  
 }  
 private ListItemCollection GetListFromSiblings(XmlNode next, ListItemCollection coll, int level)  
 {  
   level++;  
   foreach (XmlNode nod in next.ChildNodes)  
   {  
     ListItem li = new ListItem(GetSpacer(level) + nod.Attributes["name"].Value,   
       nod.Attributes["name"].Value + "" + level);  
     li.Attributes.Add("class","level" + level);  
     coll.Add(li);  
     if (nod.HasChildNodes)  
       coll = GetListFromSiblings(nod.CloneNode(true), coll, level);  
   }  
   return coll;  
 }  
 private string GetSpacer(int lev)  
 {  
   StringBuilder sb = new StringBuilder("");  
   for (int i = 0; i < lev; i++)  
   {  
     sb.Append("-");  
   }  
   return sb.ToString() + " ";  
 }  

Inga kommentarer:

Skicka en kommentar