我有以下代码,现在我在控制台窗口中获取查询输出没有任何问题,但我想将它放在 Listview 中。我不知道该怎么做.. :)
这是我的 XML 数据:
<?xml version="1.0" encoding="utf-8" ?>
<Student>
<Person name="John" city="Auckland" country="NZ" />
<Person>
<Course>GDICT-CN</Course>
<Level>7</Level>
<Credit>120</Credit>
<Date>129971035565221298</Date>
</Person>
<Person>
<Course>GDICT-CN</Course>
<Level>7</Level>
<Credit>120</Credit>
<Date>129971036040828501</Date>
</Person>
</Student>
以下是我的代码:
List<string> list1=new List<string>();
private void button1_Click(object sender, EventArgs e)
{
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
XDocument xDoc = XDocument.Load(path + "\\Student Data\\data.xml");
//IEnumerable<XElement> rows = from row in xDoc.Descendants("Person")
// where (string)row.Attribute("Course") == "BICT"
// select row;
string i = textBox1.Text;
IEnumerable<XElement> rows =
xDoc.Descendants()
.Where(d => d.Name == "Person" &&
d.Descendants().Any(e => e.Name == "ID" &&
e.Value == i)
);
foreach (XElement xEle in rows)
{
IEnumerable<XAttribute> attlist =
from att in xEle.DescendantsAndSelf().Attributes()
select att;
foreach (XAttribute xatt in attlist)
{
string n = xatt.ToString();
//Console.WriteLine(xatt);
list1.Add(n);
}
foreach (XElement elemnt in xEle.Descendants())
{
Console.WriteLine(elemnt.Value);
list1.Add(elemnt.Value);
}
//Console.WriteLine("-------------------------------------------");
}
//Console.ReadLine();
listView1.Items.Add(); // I am not sure....
}