Bind .xml file to treelistiview in objectlistview

此生再无相见时 提交于 2019-12-12 03:38:33

问题


I have to bind xml to Treelistview in Objectlistview
TestSuite.xml

<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>

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;        
    }
}

Written below code on form load to bind xml to treeview

        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);

        }

Added one column in 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});

After running in treelistview its showing error "'Name' is not a parameter-less method, property or field of type..."

I have refer below link for loading xml to treelistview

Reference link


回答1:


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);
        }

}



来源:https://stackoverflow.com/questions/42923047/bind-xml-file-to-treelistiview-in-objectlistview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!