0

私は次のコードを持っています。現在、コンソール ウィンドウで問題なくクエリ出力を取得していますが、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.... 
}
4

2 に答える 2

0

ListViewItemsに結果を追加するために作成しますListViewListViewItem文字列の配列を受け入れます。1つ目はアイテムのテキストです。その他はサブアイテムです (詳細表示モードで表示)

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);
于 2012-11-11T12:50:22.027 に答える
0

実行時に ListView に項目を追加する方法については、MSDN: http://msdn.microsoft.com/en-us/library/aa983548(v=vs.71).aspxで説明されています。

// Adds a new item with ImageIndex 3
listView1.Items.Add("List item text", 3);

画像がない場合は、インデックスを 0 に設定してください。

于 2012-11-11T12:49:49.647 に答える