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