日期:2014-05-18  浏览次数:20868 次

根据自己的xml生成树,问题解决马上结账
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<RuleUnit xmlns="Dtsc.Common.RuleEngine">
  <RuleList>
  <Rule id="Action id">
  <Condition DataType="ConditionDataType">
  <OperatorNumber DataType="OperDataType" ParameterID="OperParameterID" Value="OperValue" />
  <OperatorChar Value="OperCharValue" />
  </Condition>
  <Action IsFinallyAction="true" id="Action id">
  </Action>
  </Rule>
  <Rule id="Action id1">
  <Condition DataType="ConditionDataType1">
  <OperatorNumber DataType="OperDataType1" ParameterID="OperParameterID1" Value="OperValue1" />
  <OperatorChar Value="OperCharValue1" />
  </Condition>
  <Action IsFinallyAction="true" id="Action id1">
  </Action>
  </Rule>
  </RuleList>
</RuleUnit>
上面是我的xml文档,请问下怎么生成树,请各位大哥大姐给出代码参考,问题解决马上结账

------解决方案--------------------
我来给你解答吧
C# code

        private void button1_Click(object sender, EventArgs e)
        {
            XmlDocument ParaDoc = new XmlDocument();
            ParaDoc.Load(@"D:\test.xml");
            XmlNodeReader ParaReader = new XmlNodeReader(ParaDoc);
            
            TreeNode tn = new TreeNode();
            tn.Text = "Document";
            treeView1.Nodes.Add(tn);
            GetTreeFromXml(ParaReader, tn);
        }
        private void GetTreeFromXml(XmlNodeReader pr, TreeNode tn)
        {
            TreeNode newTn = new TreeNode();
            while(pr.Read())    
            {
                switch (pr.NodeType)
                {
                    case XmlNodeType.XmlDeclaration:
                    case XmlNodeType.Comment:
                        newTn.Text = pr.NodeType.ToString() + ":" + pr.Value ;
                        tn.Nodes.Add(newTn);
                        break;
                    case XmlNodeType.Text:
                        newTn.Text = pr.Value;
                        tn.Nodes.Add(newTn);
                        break;
                    case XmlNodeType.EndElement:
                        tn = tn.Parent;
                        break;
                    case XmlNodeType.Element:
                        if(!pr.IsEmptyElement)
                        {
                            newTn.Text=pr.Name;
                            tn.Nodes.Add(newTn);
                            if(pr.HasAttributes)
                            {
                                for(int i=0;i<pr.AttributeCount;i++)
                                {
                                    pr.MoveToAttribute(i);
                                    newTn.Text=newTn.Text+","+pr.Name+"="+pr.Value;

                                }
                                
                            }
                            tn=newTn;
                        }
                        else
                        {
                            newTn.Text="EmptyElement:"+pr.Name;
                            if(pr.HasAttributes)
                            {
                                for(int i=0;i<pr.AttributeCount;i++)
                                {
                                    pr.MoveToAttribute(i);
                                    newTn.Text=newTn.Text+","+pr.Name+"="+pr.Value;

                                }
                                tn.Nodes.Add(