代码之家  ›  专栏  ›  技术社区  ›  user6748530

将动态XML文件加载到树视图控件

  •  0
  • user6748530  · 技术社区  · 7 年前

    我是一名编程新手,我正在尝试使用C#显示一些文件的元数据。网我一直在使用Visual Studio Express 2015 for web with。NET framework 4.5.2版。

    我已经为元数据创建了几个xml文件,并在我的设计页面中放置了一个TreeView控件。您能告诉我一种从xml文件填充TreeView控件的方法吗。示例xml文件如下所示。

        <?xml version="1.0" encoding="utf-8"?>  
    <metadata>  
    <idinfo>  
    <citation>  
    <citeinfo>  
    <origin>Jay Diffendorfer</origin>  
    <origin>Roger Compton</origin>  
    <origin>Louisa Kramer</origin>  
    <origin>Zach Ancona</origin>  
    <origin>Donna Norton</origin>  
    <pubdate>201402</pubdate>  
    <title>Onshore Industrial Wind Turbine Locations for the United States through July 2013</title>  
    <geoform>Maps and Data - vector digital data</geoform>  
    <pubinfo>  
    <pubplace>Denver, CO</pubplace>  
    <publish>United States Geological Survey (USGS)</publish>  
    </pubinfo>  
    <onlink>http://dx.doi.org/10.3133/ds817 andhttp://eerscmap.er.usgs.gov/windfarm/</onlink>  
    </citeinfo>  
    </citation>  
    </idinfo>  
    </metadata>  
    

    上述xml应显示为树结构,如下所示:

     Identification Information:
              Citation:
                   Citation Information:
                              Originator: Jay Diffendorfer
                              Originator: Roger Compton
                              Originator: Louisa Kramer
                              Originator: Zach Ancona
                              Originator: Donna Norton
                              Published Date: 2014 02 
                              Title: Onshore Industrial Wind Turbine Locations for the United States through July 2013
                              Geoform:Maps and Data - vector digital data
                              Publication Information: 
                                         Published Place: Denver, CO
                                         Published by: United States Geological Survey (USGS) 
                                         Online Link:http://dx.doi.org/10.3133/ds817 andhttp://eerscmap.er.usgs.gov/windfarm/
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Kiwimanshare    7 年前

    以下是说明: Example

    下面是示例代码:

    private string _xml = @"C:\Temp\Test.xml";
    
        private void Populate()
        {
            try
            {
                // SECTION 1. Create a DOM Document and load the XML data into it.
                XmlDocument dom = new XmlDocument();
                dom.Load(_xml);
    
                // SECTION 2. Initialize the TreeView control.
                treeView1.Nodes.Clear();
                treeView1.Nodes.Add(new TreeNode(dom.DocumentElement.Name));
                TreeNode tNode = new TreeNode();
                tNode = treeView1.Nodes[0];
    
                // SECTION 3. Populate the TreeView with the DOM nodes.
                AddNode(dom.DocumentElement, tNode);
                treeView1.ExpandAll();
            }
            catch (XmlException xmlEx)
            {
                MessageBox.Show(xmlEx.Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    
        private void AddNode(XmlNode inXmlNode, TreeNode inTreeNode)
        {
            XmlNode xNode;
            TreeNode tNode;
            XmlNodeList nodeList;
            int i;
    
            // Loop through the XML nodes until the leaf is reached.
            // Add the nodes to the TreeView during the looping process.
            if (inXmlNode.HasChildNodes)
            {
                nodeList = inXmlNode.ChildNodes;
                for(i = 0; i<=nodeList.Count - 1; i++)
                {
                    xNode = inXmlNode.ChildNodes[i];
                    inTreeNode.Nodes.Add(new TreeNode(xNode.Name));
                    tNode = inTreeNode.Nodes[i];
                    AddNode(xNode, tNode);
                }
            }
            else
            {
                // Here you need to pull the data from the XmlNode based on the
                // type of node, whether attribute values are required, and so forth.
                inTreeNode.Text = (inXmlNode.OuterXml).Trim();
            }
        }                 
    

    我所做的是创建一个新的WindowsForms项目,然后在表单中添加一个TreeView控件。在代码中,我添加了使用系统的using指令。Xml:

    using System.Xml;
    

    最后,我制作了一个测试XML文件(复制了您的文件),并将一个变量添加到路径中,以便使用它(私有字符串\u XML)。