将xml doc读入树视图c#

时间:2015-09-18 09:29:42

标签: c# xml treeview

我试图在C#上将xml文件读入树视图结构。 Treeview应该将根节点显示为“学生”,并在该显示下显示每个学生编号,该编号扩展到学生的所有其他详细信息。我不确定如何正确执行此操作并集成Course表。

我编写了XML文件编码(文件名为ReadXml),如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<STUDENTS>
   <Course>
      <ID>2</ID>
      <Course_Name>Bcom Acounting</Course_Name>
      <Course_Description>For Accountants</Course_Description>
   </Course>
   <Course>
      <ID>3</ID>
      <Course_Name>Engineering</Course_Name>
      <Course_Description>For students not liking the social</Course_Description>
   </Course>
   <Course>
      <ID>1</ID>
      <Course_Name>Bcom Informatics</Course_Name>
      <Course_Description>Business Computer Science</Course_Description>
   </Course>
   <Students>
      <ID>1</ID>
      <Name>Henk</Name>
      <Initials>HW</Initials>
      <Surname>Pretorius</Surname>
      <StudentNumber>91404411</StudentNumber>
      <YearOfRegistration>2014</YearOfRegistration>
      <Gender>M</Gender>
      <Course>1</Course>
   </Students>
   <Students>
      <ID>2</ID>
      <Name>Jane</Name>
      <Initials>JL</Initials>
      <Surname>Hlope</Surname>
      <StudentNumber>22223333</StudentNumber>
      <YearOfRegistration>2014</YearOfRegistration>
      <Gender>F</Gender>
      <Course>1</Course>
   </Students>
   <Students>
      <ID>3</ID>
      <Name>Samuel</Name>
      <Initials>SM</Initials>
      <Surname>Klopper</Surname>
      <StudentNumber>12341233</StudentNumber>
      <YearOfRegistration>2014</YearOfRegistration>
      <Gender>M</Gender>
      <Course>2</Course>
   </Students>
   <Students>
      <ID>4</ID>
      <Name>Sue</Name>
      <Initials>SS</Initials>
      <Surname>Callahan</Surname>
      <StudentNumber>12333222</StudentNumber>
      <YearOfRegistration>2014</YearOfRegistration>
      <Gender>F</Gender>
      <Course>2</Course>
   </Students>
   <Students>
      <ID>5</ID>
      <Name>Koos</Name>
      <Initials>KK</Initials>
      <Surname>Klopper</Surname>
      <StudentNumber>12334455</StudentNumber>
      <YearOfRegistration>2014</YearOfRegistration>
      <Gender>M</Gender>
      <Course>1</Course>
   </Students>
</STUDENTS>

我在C#中使用的方法如下:

private void button1_Click(object sender, EventArgs e)
{
    OpenFileDialog dlg = new OpenFileDialog();
    dlg.Title = "Open XML Document";
    dlg.Filter = "XML Files (*.xml)|*.xml";
    dlg.FileName = "ReadXml.xml";
    if (dlg.ShowDialog() == DialogResult.OK)
    {
        try
        {
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(dlg.FileName);
            treeView1.Nodes.Clear();
            treeView1.Nodes.Add(new TreeNode(xDoc.DocumentElement.Name));
            TreeNode tNode = new TreeNode();
            tNode = (TreeNode)treeView1.Nodes[0];
            addTreeNode(xDoc.DocumentElement, tNode);
        }
        catch (Exception ex) 
        {
            MessageBox.Show(ex.Message);
        }
    }
}

private void addTreeNode(XmlNode xmlNode, TreeNode treeNode)
{
    XmlNode xNode;
    TreeNode tNode;
    XmlNodeList xNodeList;
    if (xmlNode.HasChildNodes) 
    {
        xNodeList = xmlNode.ChildNodes;
        for (int x = 0; x <= xNodeList.Count -1; x++)
        {
            xNode = xmlNode.ChildNodes[x];
            treeNode.Nodes.Add(new TreeNode(xNode.Name));
            tNode = treeNode.Nodes[x];
            addTreeNode(xNode, tNode);
        }
    }
    else 
        treeNode.Text = xmlNode.OuterXml.Trim();
}

我在C#中使用的代码没有带来我想要的结果。会感激一些帮助。 感谢

0 个答案:

没有答案