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

时间:2018-02-20 09:51:14

标签: c# asp.net xml treeview metadata

我是编程新手,我尝试使用C#.Net显示某些文件的元数据。我一直在使用Visual Studio Express 2015 for .NET with .NET framework version 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 个答案:

答案 0 :(得分:0)

这是一个desctiption: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项目,然后我在Form中添加了一个TreeView-Control。在代码中我添加了using指令以使用System.Xml:

using System.Xml;

最后我用一个变量将一个Test XML文件(复制了你的文件)制作成路径,这样我就可以使用它了(私有字符串_xml)。