使用XmlReader和XmlDocument 读取xml文件,使用总结

我的xml文件

< conn >

  < connstr >

    < connno > adcc0 </ connno >

     < connection_string > Server=cheshi;database=adcx0;uid=zx;pwd= 111 </ connection_string >

  </ connstr >

  < connstr >

    < connno > adcd0 </ connno >

    < connection_string > Server=DATA-cw;database=adcx0;uid=zx;pwd=111 </ connection_string >

  </ connstr >

  < connstr >

    < connno > adcx0 </ connno >

    < connection_string > Server=DATA-cw;database=adcx0;uid=zx;pwd=1111 </ connection_string >

  </ connstr >

  < connstr >

    < connno > adck0 </ connno >

    < connection_string > Server=DATA-cw;database=adcx0;uid=zx;pwd= 111 </ connection_string >

  </ connstr >

</conn>

xml文件的操作

XmlDocument xml = new XmlDocument ();

        xml.Load(Server.MapPath( "conn.xml" ));

        XmlNodeList nodeList = xml.SelectNodes( "conn//connstr" );

        string result= "" ;

        foreach ( XmlNode xn in nodeList)

        {

            Session[xn.ChildNodes.Item(0).InnerText] = xn.ChildNodes.Item(1).InnerText;

            result += Session[xn.ChildNodes.Item(0).InnerText].ToString() + "<br/>7777" ;

        }

        mEmployeesLabel.Text = result;

 

              

方案2:

               string result;

               while (reader.Read())

               {

                   if (reader.Name == "connstr" )

                   {

                       result = "" ;

                       for (int count = 0; count < reader.AttributeCount; count++)

                       {

                           result += "---" ;

                        }

                       result += "->" + reader.ReadInnerXml() + "<br/>" ;

mEmployeesLabel.Text += result;

}

xml文件作数据源:

DataSet objDataSet = new DataSet();
               objDataSet.ReadXml(Server.MapPath("people.xml"));

GridView1.DataSource = objDataSet;
               GridView1.DataBind();

 

用作数据源的xml文件

<People>
  <Person>
    <user_id>
      userid
    </user_id>
    <user_name>
      username
    </user_name>
    <sex>

    </sex>
  </Person>
</People>

 

posted @ 2008-07-29 17:36  流浪者的飘  阅读(289)  评论(0编辑  收藏  举报