0

我必须将 xml 绑定到 Objectlistview
TestSuite.xml中的 Treelistview

<TestSuite>
        <TestCase name="TestCase" UID="" State="" DataSourceId="">
            <TestModule name="Recording" State="Checked" UID=""></TestModule>
            <TestModule name="Recording1" State="Checked" UID=""></TestModule>      
        </TestCase>
        <TestCase name="TestCase" UID="" State="" DataSourceId="">
            <TestModule name="Recording" State="Checked" UID=""></TestModule>
            <TestModule name="Recording1" State="Checked" UID=""></TestModule>
        </TestCase>
    </TestSuite>

测试套件.cs

namespace ObjectListViewDemo
{
    public class TestSuite
    {
        [XmlArrayAttribute("TestCase")]
        public TestModule[] TestModules;
    }
    public class TestCase
    {
        [XmlAttribute]
        public string name;

        [XmlAttribute]
        public string UID;

        [XmlAttribute]
        public string State;

        [XmlAttribute]
        public string DataSourceId;       
    }

    public class TestModule
    {
        [XmlAttribute]
        public string name;

        [XmlAttribute]
        public string State;

        [XmlAttribute]
        public string UID;        
    }
}

写在下面的表单加载代码以将 xml 绑定到树视图

        private void TestTreeViewForm_Load(object sender, EventArgs e)
        {  
            StreamReader sr = new StreamReader(Path.Combine(@"D:\Test Suite", "TestSuite.xml"));
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(TestSuite));
            TestSuite testSuite = (TestSuite)xmlSerializer.Deserialize(sr);

            // Deserialize other XML as necessary

            List<TestSuite> TestSuiteCollection = new List<TestSuite>();
            TestSuiteCollection.Add(testSuite);           

            // Add other MyTrack objects to collection
            treeListView1.SetObjects(TestSuiteCollection);

        }

在 treelistview 中添加了一列

    this.olvColumn1.AspectName = "Name";
                this.olvColumn1.Text = "Name";
                this.olvColumn1.Width = 180;
                this.olvColumn1.WordWrap = true;
this.treeListView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
            this.olvColumn1});

在 treelistview 中运行后,它显示错误“'Name' 不是无参数方法、属性或类型的字段......”

我参考下面的链接将xml加载到treelistview

参考链接

4

1 回答 1

0
public MyForm()
        {
            InitializeComponent();

            //LoadTree();
            SetupColoumn();
            LoadTree1();
        }

        private void SetupColoumn()
        {
            // Get the size of the file system entity. 
            // Folders and errors are represented as negative numbers
            this.olvColumn1.AspectGetter = delegate(object x)
            {
                return ((XmlNode)(x)).Attributes["name"].Value;
            };
        }
        private void LoadTree1()
        {
            XmlDocument reader = new XmlDocument();
            reader.Load(@"F:\Test1.xml");
            ArrayList roots = new ArrayList();
            String xpath = "/TestSuite/TestCase";
            var nodes = reader.SelectNodes(xpath);

            foreach (XmlNode childrenNode in nodes)
            {
                roots.Add(childrenNode);
                //roots.Add(childrenNode.Attributes["Name"].Value);
            }

            this.treeListView1.CanExpandGetter = delegate(object x)
            {
                //return ((MyFileSystemInfo)x).IsDirectory;
                return ((XmlNode)x).HasChildNodes;
            };

            this.treeListView1.ChildrenGetter = delegate(object x)
            {
                ArrayList children = new ArrayList();
                var node1 = ((XmlNode)x).ChildNodes;
                if (x is XmlNode)
                {
                    //foreach (XmlNode node in roots)
                    {
                        foreach (XmlNode n in node1)
                        {
                            children.Add(n);
                        }
                    }
                }

                return children;

            };

            treeListView1.SetObjects(roots);
        }

}

于 2017-03-22T17:21:33.593 回答