No Opinions...just code

read an XML File into aTreeview

    1 private void XMLToTreeview(string thePath, XmlDocument theDocument)
    2 {
    3     String xmlPath = thePath;
    4     XmlNode xnde = theDocument.SelectSingleNode(xmlPath);
    5 
    6     try
    7     {
    8         RecurseXmlToTreeview();
    9         favoritesTree.ExpandAll();
   10     }
   11     catch (XmlException xmlEx)
   12     {
   13         MessageBox.Show(xmlEx.Message);
   14     }
   15     catch (Exception ex)
   16     {
   17         MessageBox.Show(ex.Message);
   18     }
   19 }
   20 
   21 private void RecurseXmlToTreeview()
   22 {
   23     XPathDocument xpathDoc = new XPathDocument(aFilePath);
   24     XPathNavigator xmlNav;
   25     XPathNodeIterator xmlHeadSourceSet;
   26     Boolean resetFlag = false;
   27     TreeNode rt = null;
   28 
   29     xmlNav = xpathDoc.CreateNavigator();
   30 
   31     xmlHeadSourceSet = xmlNav.Select("aNotherNode");
   32     XPathNavigator nodesNavigator = xmlHeadSourceSet.Current;
   33     XPathNodeIterator nodesText =
nodesNavigator.SelectDescendants(
XPathNodeType.Element, false);
   34 
   35     // Dim targetNode As XmlNode = destXDoc.SelectSingleNode("aNodePath")
   36 
   37     while (nodesText.MoveNext())
   38     {
   39         if (nodesText.Current.Name == "tree")
   40             continue;
   41         else if (nodesText.Current.GetAttribute("actualpath",string.Empty) == "root")
   42         {
   43             rt = new TreeNode();
   44             rt.Text = nodesText.Current.GetAttribute("name", string.Empty);
   45             rt.Tag = "root";
   46             favoritesTree.Nodes.Add(rt);
   47         }
   48         else
   49         {
   50             TreeNode tn = new TreeNode();
   51 
   52             tn.Text = nodesText.Current.GetAttribute("name", string.Empty);
   53             tn.Tag = nodesText.Current.GetAttribute("actualpath",string.Empty);
   54             tn.ImageIndex = 1;
   55             rt.Nodes.Add(tn);
   56         }
   57     }
   58 }

0 comments: