您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)asp.net如何使用DataSet的ReadXml讀取XML文件及Stream流,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
具體如下:
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字符串 用來(lái)接收WebService返回?cái)?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 后臺(tái)提交數(shù)據(jù)且獲取接口返回的數(shù)據(jù) /// <summary> /// 后臺(tái)提交數(shù)據(jù)且獲取接口返回的數(shù)據(jù) /// </summary> /// <param name="relativePath">地址</param> /// <returns></returns> public static string GetRequestString(string relativePath) { string requestUrl = relativePath; try { // 創(chuàng)建一個(gè)HTTP請(qǐng)求 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
關(guān)于“asp.net如何使用DataSet的ReadXml讀取XML文件及Stream流”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。