欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-11-29 14:35:34
本文实例讲述了asp.net使用dataset的readxml读取xml文件及stream流的方法。分享给大家供大家参考,具体如下: string strxml...

本文实例讲述了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();
  }
}

#region 接口返回的xml转换成dataset
/// <summary>
/// 返回的xml转换成dataset
/// </summary>
/// <param name="text">xml字符</param>
/// <returns></returns>
private dataset getdataset(string text)
{
  try
  {
    xmltextreader reader = new xmltextreader(new stringreader(text));
    reader.whitespacehandling = whitespacehandling.none;
    dataset ds = new dataset();
    ds.readxml(reader);
    reader.close();
    ds.dispose();
    return ds;
  }
  catch
  {
    return null;
  }
}
#endregion
#region 后台提交数据且获取接口返回的数据
/// <summary>
/// 后台提交数据且获取接口返回的数据
/// </summary>
/// <param name="relativepath">地址</param>
/// <returns></returns>
public static string getrequeststring(string relativepath)
{
  string requesturl = relativepath;
  try
  {
    // 创建一个http请求
    httpwebrequest request = (system.net.httpwebrequest)webrequest.create(requesturl);
    request.method = "get";
    streamreader jsonstream = new streamreader(request.getresponse().getresponsestream());
    string jsonobject = jsonstream.readtoend();
    return jsonobject;
  }
  catch
  {
    return string.empty;
  }
}
#endregion

更多关于asp.net相关内容感兴趣的读者可查看本站专题:《asp.net操作json技巧总结》、《asp.net字符串操作技巧汇总》、《asp.net操作xml技巧总结》、《asp.net文件操作技巧汇总》、《asp.net ajax技巧总结专题》及《asp.net缓存操作技巧总结》。

希望本文所述对大家asp.net程序设计有所帮助。