C# 使用Linq查询XML文档并将结果添加到listview中

C# 使用Linq查询XML文档并将结果添加到listview中,c#,xml,winforms,linq,linq-to-xml,C#,Xml,Winforms,Linq,Linq To Xml,我有下面的代码,现在我在控制台窗口中获得查询输出,没有任何问题,但我希望它在Listview中。我不知道怎么做……) 这是我的XML数据: <?xml version="1.0" encoding="utf-8" ?> <Student> <Person name="John" city="Auckland" country="NZ" /> <Person> <Course>GDICT-CN</Course>

我有下面的代码,现在我在控制台窗口中获得查询输出,没有任何问题,但我希望它在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>

GDICT-CN
7.
120
129971035565221298
GDICT-CN
7.
120
129971036040828501
下面是我的代码:

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.... 
}
List list1=新列表();
私有无效按钮1\u单击(对象发送者,事件参数e)
{
string path=Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
XDocument xDoc=XDocument.Load(路径+“\\Student Data\\Data.xml”);
//IEnumerable rows=来自xDoc.subjects(“Person”)中的行
//其中(字符串)row.Attribute(“课程”)=“BICT”
//选择行;
字符串i=textBox1.Text;
IEnumerable行=
xDoc.subjects()
其中(d=>d.Name==“个人”&&
d、 子体().Any(e=>e.Name==“ID”&&
e、 值==i)
);
foreach(行中的像素)
{
IEnumerable attlist=
从xEle.genderantsandself().Attributes()中的att
选择att;
foreach(attlist中的XAttribute xatt)
{
字符串n=xatt.ToString();
//控制台写入线(xatt);
列表1.添加(n);
}
foreach(xEle.subjects()中的XElement元素)
{
控制台写入线(元素值);
列表1.添加(元素值);
}
//Console.WriteLine(“-----------------------------------------”;
}
//Console.ReadLine();
listView1.Items.Add();//我不确定。。。。
}

MSDN中介绍了在运行时向列表视图添加项目:


如果没有任何图像,只需将索引设置为0。

创建
ListViewItems
即可将结果添加到
ListView
<代码>列表视图项接受字符串数组。第一个是项目的文本。其他为子项(在详细信息视图模式下可见)


我没有看到任何名为
ID
Person
的后代。您确定您的查询工作正常吗?您试图实现什么?它在控制台窗口中正常工作。现在,我正在尝试在窗体中实现它,尝试将结果添加到listview中。@raam030您如何知道项目不在listview中?是否已将列添加到列表视图?您是否设置了查看模式
详细信息
?@raam030请确保在将项目集合分配给listview后未清除该集合。在调试器中设置制动点,查看
集合是否为空。如果它是空的,那么您的xml有问题,否则listview设置有问题。
// Adds a new item with ImageIndex 3
listView1.Items.Add("List item text", 3);
var items = from p in xdoc.Descendants("Person")
            select new ListViewItem(
                new string[] {
                    (string)p.Attribute("name"),
                    (string)p.Element("Course"),
                    (string)p.Element("Level")     
                }
                );

foreach(var item in items)
     listView.Items.Add(item);