zoukankan      html  css  js  c++  java
  • 使用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>

  • 相关阅读:
    第四周编程总结
    第三周编程总结
    第二周编程总结
    第一周编程总结
    2019年寒假作业3
    2019年寒假作业2
    2019年寒假作业1
    第七周编程总结
    第六周编程总结
    第五周编程总结
  • 原文地址:https://www.cnblogs.com/luofuxian/p/2377478.html
Copyright © 2011-2022 走看看