首页新闻找找看学习计划

listview呈现xml数据 急急!!!!

0
悬赏园豆:10 [已关闭问题]

我要实现的效果是:将XML中的数据用listview呈现成表格就像gridview那样。但是出错了,我把XML文件和部分核心代码贴出来请教高手指点。

XML文件:

<?xml version="1.0" encoding="gb2312" standalone="no"?>
<table>
  <users>
    <user>
      <name>萧江涛</name>
      <sex>男</sex>
      <phone>12345678912</phone>
      <address>中国</address>
      <company>成都哈</company>
    </user>
    <user>
      <name>郭强</name>
      <sex>男</sex>
      <phone>234356543</phone>
      <address>巴中</address>
      <company>货安</company>
    </user>
    <user>
      <name>郑金龙</name>
      <sex>男</sex>
      <phone>34564342</phone>
      <address>巴中</address>
      <company>货安</company>
    </user>
  </users>
</table>

我用了两种向listview控件中写入XML的方法但是都报错了。下面是这两种方法:

  #region 加载xml到listview中显示
                XmlDocument doc = new XmlDocument();
                doc.Load("..//../xmlFile.xml");
                //定位节点
                XmlNode nodes = doc.SelectSingleNode("//users");
                //获取所有子节点
                XmlNodeList nodeList = nodes.ChildNodes;

                foreach (XmlNode Node in nodeList)
                {
                    XmlElement nodeValue = (XmlElement)Node;//转换为元素节点
                    XmlNodeList nodeValueList = nodeValue.ChildNodes;
                    foreach (XmlNode node in nodeValueList)
                    {
                        //li.SubItems.Clear();
                        li.SubItems[0].Text = Node.ChildNodes.Item(0).InnerText.ToString();
                        li.SubItems.Add(Node.ChildNodes.Item(1).InnerText.ToString());
                        li.SubItems.Add(Node.ChildNodes.Item(2).InnerText.ToString());
                        li.SubItems.Add(Node.ChildNodes.Item(3).InnerText.ToString());
                        li.SubItems.Add(Node.ChildNodes.Item(4).InnerText.ToString());
                        lv.Items.Add(li);
                    }
                }
                //this.lv.Items.Add(li);
                #endregion

                #region xml写入DataSet
                string xmlPath = "..//../xmlFile.xml";
                DataSet xmlDaSet = new DataSet();
                xmlDaSet.ReadXml(xmlPath,XmlReadMode.InferSchema);
                DataTable xmlTable = xmlDaSet.Tables["user"];
                lv.Items.Clear();//将datatable写入listview
                foreach (DataRow rows in xmlTable.Rows)
                {
                    li.SubItems[0].Text = rows["name"].ToString();
                    li.SubItems.Add(rows["sex"].ToString());
                    li.SubItems.Add(rows["phone"].ToString());
                    li.SubItems.Add(rows["address"].ToString());
                    li.SubItems.Add(rows["company"].ToString());
                    lv.Items.Add(li);
                }
                #endregion

 

我在打断点调试时发现不能给listview的第二项写入值,不知道为什么。请高手指点。有分送哦!

涛O(∩_∩)O涛的主页 涛O(∩_∩)O涛 | 初学一级 | 园豆:190
提问于:2010-01-08 19:57
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册