代码错误:加载XML文档

时间:2009-03-03 08:36:13

标签: c# xml winforms

这是参考前面提到的问题。这是完整的代码,它给了我一个错误:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Xml;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO;

namespace LoadXMLtreeDisplay
{
    public partial class TreeDisplay : Form
    {
        public string Filename
        {
            get { return filename; }
        }
        protected string filename;
        public TreeDisplay()
        {
            InitializeComponent();
            this.Text = "Tree View of XML File";//Form Title.
        }
       private void treeDocLoadMethod(string nameofFile)
        {
                XmlDocument xdoc = new XmlDocument();
                xdoc.Load(nameofFile);
                this.treeView1.Nodes.Clear();
                this.treeView1.Nodes.Add(new TreeNode(xdoc.DocumentElement.Name));
                TreeNode tNodeObj = new TreeNode();
                tNodeObj = this.treeView1.Nodes[0];
                XmlNode xNode = xdoc.DocumentElement;
                AddingNodesToTree(tNodeObj, xNode);
                treeView1.Nodes[0].Expand();
                treeView1.CollapseAll();
        }//treeDocLoadMethod

        private void AddingNodesToTree(refXmlNode xnode,TreeNode tnode)
        {

            TreeNode subNode = tnode.Add(xnode.Name);
            subNode.Tag = xnode;
            foreach (XmlNode subElement in xnode.ChildNodes)
            {
                AddingNodesToTree(ref subElement, ref subNode);
            }       

        } 
        private void treeView1_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
        { listBox1.Items.Clear();
            TreeNode currentNode = e.Node;
            XmlElement currentElement = (XmlElement)currentNode.Tag;
            XmlAttributeCollection attCol = currentElement.Attributes;
            foreach (XmlAttribute xmlatt in attCol)
            {
                listBox1.Items.Show(xmlatt);
            }
         }

//Rest of the code is for winform display(various buttons and boxes)
     }
}

编译完成后我收到错误:

  

期望的课程,代表,enum,   接口,或结构类型或   命名空间定义或文件结束   预期

请您告诉我可能的错误来源是什么以及如何纠正错误?我是C#的新手

1 个答案:

答案 0 :(得分:1)

第一行缺少一个空间;应该是“ref XmlNode”而不是“refXmlNode”

编辑:不是第一行(不再是);但是说:

private void AddingNodesToTree(refXmlNode xnode,TreeNode tnode)