呵呵...楼上的。你的XML文件格式已经固定了吗?我这边倒是有个例子的。贴出来给你,我没有调试(网上找的)。你循着这个例子的思路,应该可以搞定了。哪个安装程序怎么样了啊?
using System;
using System.Windows.Forms;
using System.Xml;
using System.Globalization;namespace TreeSample
{
/// <summary>
/// Summary description for TreeFunctions.
/// </summary>
public class TreeFunctions
{
public TreeFunctions()
{
//Add constructor logic here
} internal System.Xml.XmlDocument Tree2XML(ref System.Windows.Forms.TreeView oTreeView)
{
System.Text.StringBuilder strOutput = new System.Text.StringBuilder();
strOutput.Append("<?xml version=\"1.0\"?>\n");
strOutput.Append("<hierarchy>\n");
WalkTreeNodes(oTreeView.Nodes, ref strOutput);
strOutput.Append("</hierarchy>"); System.Xml.XmlDocument oXML = new System.Xml.XmlDocument();
try
{
oXML.LoadXml(strOutput.ToString());
}
catch(System.Xml.XmlException excXML)
{
MessageBox.Show("Cannot Load XML");
Application.Exit();
}
return oXML;
} private void WalkTreeNodes(System.Windows.Forms.TreeNodeCollection xNodeCol, ref System.Text.StringBuilder strOut)
{
foreach(EnhancedTreeNode xNode in xNodeCol)
{
strOut.Append("<node id=\"" + xNode.ItemID.ToString(CultureInfo.InvariantCulture) + "\" position=\"" + xNode.Index + "\">\n");
strOut.Append("<![CDATA[" + xNode.ItemName + "]]>\n");
if(xNode.GetNodeCount(true)>0)
{
WalkTreeNodes(xNode.Nodes, ref strOut);
}
strOut.Append("</node>\n");
}
}

internal void CreateTree(string strHierarchy, System.Windows.Forms.TreeNodeCollection xTreeNodeCol)
{
System.Xml.XmlTextReader xtr = new System.Xml.XmlTextReader(
new System.IO.StringReader(strHierarchy));
int LastDepth = -1;
int CurrentDepth = -1;
EnhancedTreeNode xNode = null;
TreeNodeCollection xCol = null;
xtr.WhitespaceHandling = WhitespaceHandling.None;
while(xtr.Read())
{
if(xtr.NodeType==XmlNodeType.Element)
{
switch(xtr.Name)
{
case "node":
xNode = new EnhancedTreeNode();
CurrentDepth = xtr.Depth;
xtr.MoveToAttribute("id");
xtr.ReadAttributeValue();
xNode.ItemID = Convert.ToInt32(xtr.Value, CultureInfo.InvariantCulture);
xtr.MoveToElement();
xtr.MoveToAttribute("inactive");
xtr.ReadAttributeValue();
if(LastDepth==-1) //it's the root!
{
xCol = xTreeNodeCol;
}
else if(CurrentDepth<LastDepth) //Move Up A Node
{
for(int i=0;i<LastDepth-CurrentDepth;i++)
{
xCol = xCol[xCol.Count-1].Parent.Parent.Nodes;
}
}
else if(CurrentDepth==LastDepth) //Stay in the same collection
{
xCol = xCol;
}
else if(CurrentDepth>LastDepth) // Move Down A Node
{
for(int i=0;i<CurrentDepth-LastDepth;i++)
{
xCol = xCol[xCol.Count-1].Nodes;
}
}
xtr.MoveToElement();
LastDepth = xtr.Depth;
break;
case "name":
xNode.ItemName = xtr.ReadString();
xCol.Add(xNode);
break;
default:
break;
}
}
}
xtr.Close();
}
}
public sealed class EnhancedTreeNode : System.Windows.Forms.TreeNode
{
private int m_intItemID = -1;
private string m_strItemName = string.Empty; public EnhancedTreeNode()
{
//Add constructor logic here
} public EnhancedTreeNode(string ItemName, int ItemID)
{
m_intItemID = ItemID;
m_strItemName = ItemName;
this.Text = ItemName;
} public int ItemID
{
get{return m_intItemID;}
set{m_intItemID=value;}
} public string ItemName
{
get{return m_strItemName;}
set{m_strItemName=value;this.Text=m_strItemName;}
}
}
另外XML文件的内容<?xml version=1.0>
<hierarchy>
<node inactive="N" position="0" id="0">
<name>Catalog</name>
<node inactive="N" position="0" id="1">
<name>Category A</name>
<node inactive="N" position="0" id="2">
<name>Item 1</name>
</node>
<node inactive="N" position="1" id="3">
<name>Item 2</name>
</node>
</node>
<node inactive="N" position="0" id="4">
<name>Category B</name>
<node inactive="N" position="0" id="5">
<name>Item 3</name>
</node>
<node inactive="N" position="1" id="6">
<name>Item 4</name>
</node>
</node>
</node>
</hierarchy>