濮阳杆衣贸易有限公司

主頁 > 知識庫 > asp.net使用DataSet的ReadXml讀取XML文件及Stream流的方法

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

熱門標簽:電話機器人鑰匙扣 400電話唐山辦理 旅游地圖標注線路 地圖標注位置怎么弄圖 威力最大的電銷機器人 電銷外呼系統(tǒng)是違法的嗎 廣西房產(chǎn)智能外呼系統(tǒng)推薦 漯河外呼調(diào)研線路 電銷專用外呼線路

本文實例講述了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>RB/genre>
            album>unknown/album>
            year>/year>
           /song>
           song>
            artist>Babyface/artist>
            genre>RB/genre>
            album>The essential babyface/album>
            year>2001/year>
           /song>
           song>
            artist>Babyface/artist>
            genre>RB/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>RB/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返回數(shù)據(jù)
    ds.ReadXml(reader, XmlReadMode.Auto);
    //生成Xml文件
    //ds.WriteXml(Server.MapPath("xml/song_bak.xml"));
    GridView1.DataSource = ds;
    GridView1.DataBind();
  }
}

#region 接口返回的Xml轉(zhuǎn)換成DataSet
/// summary>
/// 返回的Xml轉(zhuǎn)換成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 后臺提交數(shù)據(jù)且獲取接口返回的數(shù)據(jù)
/// summary>
/// 后臺提交數(shù)據(jù)且獲取接口返回的數(shù)據(jù)
/// /summary>
/// param name="relativePath">地址/param>
/// returns>/returns>
public static string GetRequestString(string relativePath)
{
  string requestUrl = relativePath;
  try
  {
    // 創(chuàng)建一個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相關內(nèi)容感興趣的讀者可查看本站專題:《asp.net操作json技巧總結(jié)》、《asp.net字符串操作技巧匯總》、《asp.net操作XML技巧總結(jié)》、《asp.net文件操作技巧匯總》、《asp.net ajax技巧總結(jié)專題》及《asp.net緩存操作技巧總結(jié)》。

希望本文所述對大家asp.net程序設計有所幫助。

您可能感興趣的文章:
  • asp.net 讀取xml文件里面的內(nèi)容,綁定到dropdownlist中
  • ASP.NET讀取XML文件4種方法分析
  • Javascript+XMLHttpRequest+asp.net無刷新讀取數(shù)據(jù)庫數(shù)據(jù)
  • ASP.NET中讀取XML文件信息的4種方法與示例代碼
  • ASP.NET MVC DropDownList數(shù)據(jù)綁定及使用詳解
  • ASP.NET MVC中為DropDownListFor設置選中項的方法
  • ASP.NET Ajax級聯(lián)DropDownList實現(xiàn)代碼
  • asp.net DropDownList自定義控件,讓你的分類更清晰
  • ASP.NET DropDownList控件的使用方法
  • (asp.net c#)DropDownList綁定后顯示對應的項的兩種方法
  • ASP.NET筆記之 ListView 與 DropDownList的使用
  • asp.net實現(xiàn)XML文件讀取數(shù)據(jù)綁定到DropDownList的方法

標簽:湖北 無錫 焦作 綏化 欽州 銅陵 試駕邀約 湘西

巨人網(wǎng)絡通訊聲明:本文標題《asp.net使用DataSet的ReadXml讀取XML文件及Stream流的方法》,本文關鍵詞  asp.net,使用,DataSet,的,ReadXml,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權(quán)與本站無關。
  • 相關文章
  • 下面列出與本文章《asp.net使用DataSet的ReadXml讀取XML文件及Stream流的方法》相關的同類信息!
  • 本頁收集關于asp.net使用DataSet的ReadXml讀取XML文件及Stream流的方法的相關信息資訊供網(wǎng)民參考!
  • 推薦文章
    抚远县| 佛山市| 城市| 台北市| 苏州市| 穆棱市| 临沭县| 蓝山县| 靖西县| 余庆县| 屯留县| 扶风县| 黔东| 阿拉善右旗| 宝清县| 麟游县| 扶余县| 凤冈县| 兴安县| 司法| 鲜城| 灵台县| 武平县| 定西市| 澄城县| 延川县| 文成县| 湘潭县| 斗六市| 临邑县| 汶川县| 油尖旺区| 城市| 兰州市| 府谷县| 沂南县| 邵阳市| 高密市| 和田县| 兴安盟| 于田县|