首页 > 编程 > .NET > 正文

asp.net使用DataSet的ReadXml读取XML文件及Stream流的方法

2024-07-10 12:47:01
字体:
来源:转载
供稿:网友

本文实例讲述了asp.net使用DataSet的ReadXml读取XML文件及Stream流的方法。,具体如下:

string strxml = "<xml><m><a>1</a><b>2</b></m><m><a>11</a><b>22</b></m><m><a>111</a><b>222</b></m></xml>";DataSet ds = new DataSet();Stream stream = new MemoryStream(Encoding.Default.GetBytes(strxml));ds.ReadXml(stream);GridView1.DataSource = ds;GridView1.DataBind();
protected void Page_Load(object sender, EventArgs e){  if (!IsPostBack)  {    DataSet ds = new DataSet();    TextReader reader = new StringReader(@"          <music>           <song>            <artist>The Chi-lites</artist>            <genre>Soul</genre>            <album>A lonely man</album>            <year>1972</year>           </song>           <song>            <artist>Babyface</artist>            <genre>R&B</genre>            <album>unknown</album>            <year></year>           </song>           <song>            <artist>Babyface</artist>            <genre>R&B</genre>            <album>The essential babyface</album>            <year>2001</year>           </song>           <song>            <artist>Babyface</artist>            <genre>R&B</genre>            <album>Grown and sexy</album>            <year>2005</year>           </song>           <song>            <artist>Maria Arredondo</artist>            <genre>Pop</genre>            <album>Not going under</album>            <year>2004</year>           </song>           <song>            <artist>Leona Lewis</artist>            <genre>Pop</genre>            <album>Unknown</album>            <year>2008</year>           </song>           <song>            <artist>Usher</artist>            <genre>R&B</genre>            <album>Usher</album>            <year>2008</year>           </song>           <song>            <artist>Christina Aguilera</artist>            <genre>Blues</genre>            <album>Back to basics</album>            <year>2004</year>           </song>           <song>            <artist>Sting</artist>            <genre>Pop</genre>            <album>Shape of my heart</album>            <year></year>           </song>          </music>          ");    //读取Xml字符串 用来接收WebService返回数据    ds.ReadXml(reader, XmlReadMode.Auto);    //生成Xml文件    //ds.WriteXml(Server.MapPath("xml/song_bak.xml"));    GridView1.DataSource = ds;    GridView1.DataBind();  }}            
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表