本文介绍了将.xml文件绑定到objectlistview中的treelistiview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须将xml绑定到Objectlistview中的Treelistview
TestSuite.xml
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;
}
}
在表单加载时将以下代码写成将xml绑定到treeview
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);
}
在treelistview中添加了一列
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});
在treelistview中运行后,显示错误'Name'不是无参数的方法,属性或类型的字段..."
After running in treelistview its showing error "'Name' is not a parameter-less method, property or field of type..."
我有下面的链接,用于将xml加载到treelistview
I have refer below link for loading xml to treelistview
推荐答案
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);
}
}
这篇关于将.xml文件绑定到objectlistview中的treelistiview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!