DataSource.xml文件,要放在bin/debug/目录下:
1 <?xml version="1.0" encoding="utf-8" ?>
2 <DataSource>
3 <type>
4 <ID>001</ID>
5 <name>张三</name>
6 </type>
7 <type>
8 <ID>002</ID>
9 <name>李四</name>
10 </type>
11 <type>
12 <ID>003</ID>
13 <name>王五</name>
14 </type>
15 </DataSource>
CS代码:
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9
10 namespace 超市收银Demo
11 {
12 public partial class Main : Form
13 {
14 public Main()
15 {
16 InitializeComponent();
17 }
18
19 private void button_loadXML_Click(object sender, EventArgs e)
20 {
21 this.listBox1.Items.Clear();
22 DataSet set = new DataSet();
23 set.ReadXml(Application.StartupPath + "\DataSource.xml");
24 foreach (DataRowView view in set.Tables[0].DefaultView)
25 {
26 this.listBox1.Items.Add(view["ID"].ToString() + " " + view["name"].ToString());
27 }
28 this.listBox1.SelectedIndex = 0;
29 }
30
31 private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
32 {
33 string value = this.listBox1.SelectedItem.ToString();
34 this.textBox1.Text = value;
35 }
36 }
37 }
运行结果: