当前位置:编程学习 > C#/ASP.NET >>

xml解析,在dataGridView中显示时数据有误


private void Main_Load(object sender, EventArgs e)
        {
            Entity.Entity_My_Password entity_my_password = new Entity.Entity_My_Password();//实体
            XmlDocument xmlDoc = new XmlDocument();
            List<Entity.Entity_My_Password> pwdList = new List<Entity.Entity_My_Password>();

            ListViewItem item = new ListViewItem();
            try
            {
                String xmlPath = "../../Data/My_Password.xml";
                xmlDoc.Load(xmlPath);
            }
            catch (Exception ex)
            {
                MessageBox.Show("文件加载失败:" + ex.Message);
                return;
            }
            XmlElement elem = xmlDoc.DocumentElement;
            foreach (XmlNode node in elem.ChildNodes)
            {
                entity_my_password.PwdName = node["pwdName"].InnerText;
                entity_my_password.PwdValue = node["pwdValue"].InnerText;
                entity_my_password.CreateDate = node["createDate"].InnerText;
                pwdList.Add(entity_my_password);
                dataGridView.DataSource = pwdList;
            }
        }


xml的结构

<?xml version="1.0" encoding="utf-8" ?>
<My_Password>
  <Pwd>
    <pwdName>密码1</pwdName>
    <pwdValue>123</pwdValue>
    <createDate>2013-6-8 14:59:21</createDate>
  </Pwd>
  <Pwd>
    <pwdName>密码2</pwdName>
    <pwdValue>acd</pwdValue>
    <createDate>2013-6-8 14:59:27</createDate>
  </Pwd>
  <Pwd>
    <pwdName>密码3</pwdName>
    <pwdValue>sty231</pwdValue>
    <createDate>2013-6-8 15:12:30</createDate>
  </Pwd>
</My_Password>



新人小白,单步走时list获取没问题,但在DataSource添加发现list出现重复的列,是新数据吧之前的列覆盖了?
很疑惑,感谢楼下的回帖 XML DataGridView
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,