C#中的listview绑定 xml文件中的数据并显示

xml:文件内容
<?xml version="1.0" encoding="utf-8" ?>
<employees>
<nol>
  <name> ma jin hu</name>
  <zip> 239900</zip>
  <address> feng huang street </address>
  <city> chu zhou</city>
</nol>
<nol2>
    <name> wang tian </name>
    <zip> 10000</zip>
    <address>  dfd  street </address>
    <city> wu  han</city>
  </nol2>
</employees>

点击的button控件中的内容:
private void button1_Click(object sender, EventArgs e)
        {
            ListViewItem myitem = new ListViewItem();

            listView1.View = View.Details;//listView显示方式  这很重要不然不显示出完全的内容


            listView1.Columns.Clear();
            listView1.Items.Clear();
            listView1.Columns.Add("name",200,HorizontalAlignment.Left);
            listView1.Columns.Add("zip",100,HorizontalAlignment.Left);
            listView1.Columns.Add("address", 100, HorizontalAlignment.Left);
            listView1.Columns.Add("city", 100, HorizontalAlignment.Left);

            string s = "";
            XmlNodeReader reader = null;
           
            try
            {
                XmlDocument doc = new XmlDocument();
                //label1.Text = Application.StartupPath;
                doc.Load("../../data.xml");//xml文件的路径
                reader = new XmlNodeReader(doc);
                while (reader.Read())//从流中读出下一个节点
                {
                    switch (reader.NodeType)
                    {
                        case XmlNodeType.Attribute:
                            break;
                        case XmlNodeType.CDATA:
                            break;
                        case XmlNodeType.Comment:
                            break;
                        case XmlNodeType.Document:
                            break;
                        case XmlNodeType.DocumentFragment:
                            break;
                        case XmlNodeType.DocumentType:
                            break;
                        case XmlNodeType.Element:
                            s = reader.Name;
                            break;
                        case XmlNodeType.EndElement:
                            break;
                        case XmlNodeType.EndEntity:
                            break;
                        case XmlNodeType.Entity:
                            break;
                        case XmlNodeType.EntityReference:
                            break;
                        case XmlNodeType.None:
                            break;
                        case XmlNodeType.Notation:
                            break;
                        case XmlNodeType.ProcessingInstruction:
                            break;
                        case XmlNodeType.SignificantWhitespace:
                            break;
                        case XmlNodeType.Text:
                            if (s.Equals("name"))
                            {
                                myitem = listView1.Items.Add(reader.Value);//当标签名称为那么的时候添加新的一行 并且将标签中的值写在第一列
                            }
                            else
                            {
                                myitem.SubItems.Add(reader.Value);
                            }
                            break;
                        case XmlNodeType.Whitespace:
                            break;
                        case XmlNodeType.XmlDeclaration:
                            break;
                        default:
                            break;
                    }
                }
            }
            finally
            {

                if (reader!=null)
                {
                    reader.Close();
                }
            }
           

          

        }

你可能感兴趣的:(ListView)