XML内容如下,我现在想把searchCount下面每个add的key,value读取进DataTable.
<?xml version="1.0" encoding="utf-8" ?>
<items>
  <searchCount>
    <add key="10" value="10" />
    <add key="20" value="20" />
    <add key="30" value="30" />
    <add key="40" value="40" />
  </searchCount>
</items>

解决方案 »

  1.   

    http://blog.csdn.net/johnsuna/archive/2007/10/29/1853844.aspx
      

  2.   

       
                DataSet ds = new DataSet();
                ds.ReadXml(Server.MapPath("a.xml"));
                this.GridView2.DataSource = ds.Tables[1].DefaultView;
                this.GridView2.DataBind();
      

  3.   

    .innerXML //应该是这么写的
      

  4.   

    我要的是searchCount节点下的....
      

  5.   

    xml本身就可以作为数据源用来读取。如2楼那样
      

  6.   

    如果没有items节点, 二楼的当然可以,
    但是,现在...
      

  7.   


    public string ReadAttr(string strXmlFilePath,string strKey)
    {
                String strRlt = String.Empty;            XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(strXmlFilePath);
                XmlNodeList nodeList = xmlDoc.SelectNodes("a.xml");            foreach(XmlNode node in nodeList )      ---迭代node对象
                {
                    if (node.Attributes.Count > 0)            ---Attributes是属性集合
                    {
                        if (node.Attributes[0].Value == strKey)
                        {
                            strRlt = node.Attributes[1].Value;
                            break;
                        }
                    }
                }            return strRlt;
    }
    参考一下
      

  8.   

    是可以的,有items节点,使用的是Tables[1],如果没有这个节点就是Tables[0]了
      

  9.   

    3种方法 :
    一.最简单的    public DataTable GetData1()
        {
            DataSet ds = new DataSet();
            ds.ReadXml(HttpContext.Current.Server.MapPath(xmlPath));
            return ds.Tables["add"];
        }二.最好理解的    public DataTable GetData2()
        {
            DataTable tb = new DataTable();
            tb.TableName = "data";
            tb.Columns.Add(new DataColumn("key", typeof(string)));
            tb.Columns.Add(new DataColumn("value", typeof(string)));        XmlDocument doc = new XmlDocument();        doc.Load(HttpContext.Current.Server.MapPath(xmlPath));        XmlNodeList nodelist = doc.DocumentElement.SelectNodes("//add");
            foreach (XmlNode node in nodelist)
            {
                DataRow r = tb.NewRow();
                r[0] = node.Attributes["key"].Value;
                r[1] = node.Attributes["value"].Value;
                tb.Rows.Add(r);
            }        doc = null;
            return tb;
        }三.文件比较大时,效率最高的    public DataTable GetData()
        {
            DataTable tb = new DataTable();
            tb.TableName = "data";
            tb.Columns.Add(new DataColumn("key", typeof(string)));
            tb.Columns.Add(new DataColumn("value", typeof(string)));        using (XmlReader reader = new XmlTextReader(HttpContext.Current.Server.MapPath(xmlPath)))
            {
                while (reader.Read())
                {
                    //如果节点为名add的元素节点
                    if (reader.NodeType == XmlNodeType.Element && reader.Name == "add")
                    {
                        DataRow r = tb.NewRow();
                        r[0] = reader.GetAttribute(0);
                        r[1] = reader.GetAttribute(1);
                        tb.Rows.Add(r);
                    }
                }
                reader.Close();
            }
            return tb;
        }