本文实例讲述了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返回数据
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程序设计有所帮助。
您可能感兴趣的文章:- asp.net 读取xml文件里面的内容,绑定到dropdownlist中
- ASP.NET读取XML文件4种方法分析
- Javascript+XMLHttpRequest+asp.net无刷新读取数据库数据
- ASP.NET中读取XML文件信息的4种方法与示例代码
- ASP.NET MVC DropDownList数据绑定及使用详解
- ASP.NET MVC中为DropDownListFor设置选中项的方法
- ASP.NET Ajax级联DropDownList实现代码
- asp.net DropDownList自定义控件,让你的分类更清晰
- ASP.NET DropDownList控件的使用方法
- (asp.net c#)DropDownList绑定后显示对应的项的两种方法
- ASP.NET笔记之 ListView 与 DropDownList的使用
- asp.net实现XML文件读取数据绑定到DropDownList的方法