DDR爱好者之家 Design By 杰米
MemoryStream对象提供了无需进行IO就可以创建Stream的方法,XmlTextWriter和XmlReader提供快速书写和读取XML内容的方法,结合MemoryStream,就可以直接在内存中构造XmlTextWriter,并用XmlReader进行读取。
使用MemoryStream和XmlTextWriter进行书写XML,需要注意两点:XmlTextWriter.Flush操作和重设MemoryStream.Position = 0。
C#
<%@ Page Language="C#"%>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.IO" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Button1_Click(object sender, EventArgs e)
{
Response.Clear();
Response.ContentType = "text/xml";
MemoryStream msXml = new MemoryStream();
XmlTextWriter xmlWriter = new XmlTextWriter(msXml, Encoding.UTF8);
xmlWriter.WriteStartElement("rss");
xmlWriter.WriteAttributeString("version", "2.0");
xmlWriter.WriteStartElement("channel");
xmlWriter.WriteElementString("title", "【孟宪会之精彩世界】");
xmlWriter.WriteElementString("link", "http://dotnet.aspx.cc/Rss.aspx");
xmlWriter.WriteElementString("description", "NET开发技术。");
xmlWriter.WriteElementString("language", "zh-CN");
xmlWriter.WriteElementString("copyright", "Copyright 1999-2007【孟宪会之精彩世界】");
xmlWriter.WriteElementString("managingEditor", "amxh[AT]21cn.com");
xmlWriter.WriteStartElement("image");
xmlWriter.WriteElementString("title", "【孟宪会之精彩世界】");
xmlWriter.WriteElementString("width", "144");
xmlWriter.WriteElementString("height", "35");
xmlWriter.WriteElementString("link", "http://dotnet.aspx.cc/");
xmlWriter.WriteElementString("url", "http://dotnet.aspx.cc/Images/LogoRss.gif");
xmlWriter.WriteEndElement();
//循环读出数据库内容列表,忽略
//while (objReader.Read())
//{
// xmlWriter.WriteStartElement("item");
// xmlWriter.WriteElementString("title", objReader.GetString(0));
// xmlWriter.WriteElementString("description", objReader.GetString(1));
// xmlWriter.WriteElementString("link", "http://dotnet.aspx.cc/article/a933b187-06c3-4263-9eec-414a54d9c815/read.aspx");
// xmlWriter.WriteElementString("pubDate", objReader.GetDateTime(3).ToString("G"));
// xmlWriter.WriteEndElement();
//}
//objReader.Close();
xmlWriter.WriteStartElement("item");
xmlWriter.WriteElementString("title", "ASP.NET 2.0中直接将Access数据库导入到Excel文件中");
xmlWriter.WriteElementString("description", "ASP.NET 2.0中直接将Access数据库导入到Excel文件中");
xmlWriter.WriteElementString("link", "http://dotnet.aspx.cc/article/a933b187-06c3-4263-9eec-414a54d9c815/read.aspx");
xmlWriter.WriteElementString("pubDate", "Sat, 02 Dec 2006 09:39:58 GMT");
xmlWriter.WriteEndElement();
xmlWriter.WriteEndElement();
xmlWriter.WriteEndElement();
xmlWriter.Flush(); // 确保书写器更新到Stream中;
msXml.Position = 0; // 重置流的位置,以便我们可以从头读取
XmlReader xmlReader = XmlReader.Create(msXml);
while (xmlReader.Read())
{
if (xmlReader.Name == "rss")
{
Response.Write(xmlReader.ReadOuterXml());
}
}
Response.End();
msXml.Close();
xmlWriter.Close();
xmlReader.Close();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>XmlReader 读取器读取内存流 MemoryStream 的注意事项</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="读取数据" />
</form>
</body>
</html>
VB.NET
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
Response.Clear
Response.ContentType = "text/xml"
Dim msXml As MemoryStream = New MemoryStream
Dim xmlWriter As XmlTextWriter = New XmlTextWriter(msXml, Encoding.UTF8)
xmlWriter.WriteStartElement("rss")
xmlWriter.WriteAttributeString("version", "2.0")
xmlWriter.WriteStartElement("channel")
xmlWriter.WriteElementString("title", "【孟宪会之精彩世界】")
xmlWriter.WriteElementString("link", "http://dotnet.aspx.cc/Rss.aspx")
xmlWriter.WriteElementString("description", "NET开发技术。")
xmlWriter.WriteElementString("language", "zh-CN")
xmlWriter.WriteElementString("copyright", "Copyright 1999-2007【孟宪会之精彩世界】")
xmlWriter.WriteElementString("managingEditor", "amxh[AT]21cn.com")
xmlWriter.WriteStartElement("image")
xmlWriter.WriteElementString("title", "【孟宪会之精彩世界】")
xmlWriter.WriteElementString("width", "144")
xmlWriter.WriteElementString("height", "35")
xmlWriter.WriteElementString("link", "http://dotnet.aspx.cc/")
xmlWriter.WriteElementString("url", "http://dotnet.aspx.cc/Images/LogoRss.gif")
xmlWriter.WriteEndElement
xmlWriter.WriteStartElement("item")
xmlWriter.WriteElementString("title", "ASP.NET 2.0中直接将Access数据库导入到Excel文件中")
xmlWriter.WriteElementString("description", "ASP.NET 2.0中直接将Access数据库导入到Excel文件中")
xmlWriter.WriteElementString("link", "http://dotnet.aspx.cc/article/a933b187-06c3-4263-9eec-414a54d9c815/read.aspx")
xmlWriter.WriteElementString("pubDate", "Sat, 02 Dec 2006 09:39:58 GMT")
xmlWriter.WriteEndElement
xmlWriter.WriteEndElement
xmlWriter.WriteEndElement
xmlWriter.Flush
msXml.Position = 0
Dim xmlReader As XmlReader = XmlReader.Create(msXml)
While xmlReader.Read
If xmlReader.Name = "rss" Then
Response.Write(xmlReader.ReadOuterXml)
End If
End While
Response.End
msXml.Close
xmlWriter.Close
xmlReader.Close
End Sub
使用MemoryStream和XmlTextWriter进行书写XML,需要注意两点:XmlTextWriter.Flush操作和重设MemoryStream.Position = 0。
C#
<%@ Page Language="C#"%>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.IO" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Button1_Click(object sender, EventArgs e)
{
Response.Clear();
Response.ContentType = "text/xml";
MemoryStream msXml = new MemoryStream();
XmlTextWriter xmlWriter = new XmlTextWriter(msXml, Encoding.UTF8);
xmlWriter.WriteStartElement("rss");
xmlWriter.WriteAttributeString("version", "2.0");
xmlWriter.WriteStartElement("channel");
xmlWriter.WriteElementString("title", "【孟宪会之精彩世界】");
xmlWriter.WriteElementString("link", "http://dotnet.aspx.cc/Rss.aspx");
xmlWriter.WriteElementString("description", "NET开发技术。");
xmlWriter.WriteElementString("language", "zh-CN");
xmlWriter.WriteElementString("copyright", "Copyright 1999-2007【孟宪会之精彩世界】");
xmlWriter.WriteElementString("managingEditor", "amxh[AT]21cn.com");
xmlWriter.WriteStartElement("image");
xmlWriter.WriteElementString("title", "【孟宪会之精彩世界】");
xmlWriter.WriteElementString("width", "144");
xmlWriter.WriteElementString("height", "35");
xmlWriter.WriteElementString("link", "http://dotnet.aspx.cc/");
xmlWriter.WriteElementString("url", "http://dotnet.aspx.cc/Images/LogoRss.gif");
xmlWriter.WriteEndElement();
//循环读出数据库内容列表,忽略
//while (objReader.Read())
//{
// xmlWriter.WriteStartElement("item");
// xmlWriter.WriteElementString("title", objReader.GetString(0));
// xmlWriter.WriteElementString("description", objReader.GetString(1));
// xmlWriter.WriteElementString("link", "http://dotnet.aspx.cc/article/a933b187-06c3-4263-9eec-414a54d9c815/read.aspx");
// xmlWriter.WriteElementString("pubDate", objReader.GetDateTime(3).ToString("G"));
// xmlWriter.WriteEndElement();
//}
//objReader.Close();
xmlWriter.WriteStartElement("item");
xmlWriter.WriteElementString("title", "ASP.NET 2.0中直接将Access数据库导入到Excel文件中");
xmlWriter.WriteElementString("description", "ASP.NET 2.0中直接将Access数据库导入到Excel文件中");
xmlWriter.WriteElementString("link", "http://dotnet.aspx.cc/article/a933b187-06c3-4263-9eec-414a54d9c815/read.aspx");
xmlWriter.WriteElementString("pubDate", "Sat, 02 Dec 2006 09:39:58 GMT");
xmlWriter.WriteEndElement();
xmlWriter.WriteEndElement();
xmlWriter.WriteEndElement();
xmlWriter.Flush(); // 确保书写器更新到Stream中;
msXml.Position = 0; // 重置流的位置,以便我们可以从头读取
XmlReader xmlReader = XmlReader.Create(msXml);
while (xmlReader.Read())
{
if (xmlReader.Name == "rss")
{
Response.Write(xmlReader.ReadOuterXml());
}
}
Response.End();
msXml.Close();
xmlWriter.Close();
xmlReader.Close();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>XmlReader 读取器读取内存流 MemoryStream 的注意事项</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="读取数据" />
</form>
</body>
</html>
VB.NET
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
Response.Clear
Response.ContentType = "text/xml"
Dim msXml As MemoryStream = New MemoryStream
Dim xmlWriter As XmlTextWriter = New XmlTextWriter(msXml, Encoding.UTF8)
xmlWriter.WriteStartElement("rss")
xmlWriter.WriteAttributeString("version", "2.0")
xmlWriter.WriteStartElement("channel")
xmlWriter.WriteElementString("title", "【孟宪会之精彩世界】")
xmlWriter.WriteElementString("link", "http://dotnet.aspx.cc/Rss.aspx")
xmlWriter.WriteElementString("description", "NET开发技术。")
xmlWriter.WriteElementString("language", "zh-CN")
xmlWriter.WriteElementString("copyright", "Copyright 1999-2007【孟宪会之精彩世界】")
xmlWriter.WriteElementString("managingEditor", "amxh[AT]21cn.com")
xmlWriter.WriteStartElement("image")
xmlWriter.WriteElementString("title", "【孟宪会之精彩世界】")
xmlWriter.WriteElementString("width", "144")
xmlWriter.WriteElementString("height", "35")
xmlWriter.WriteElementString("link", "http://dotnet.aspx.cc/")
xmlWriter.WriteElementString("url", "http://dotnet.aspx.cc/Images/LogoRss.gif")
xmlWriter.WriteEndElement
xmlWriter.WriteStartElement("item")
xmlWriter.WriteElementString("title", "ASP.NET 2.0中直接将Access数据库导入到Excel文件中")
xmlWriter.WriteElementString("description", "ASP.NET 2.0中直接将Access数据库导入到Excel文件中")
xmlWriter.WriteElementString("link", "http://dotnet.aspx.cc/article/a933b187-06c3-4263-9eec-414a54d9c815/read.aspx")
xmlWriter.WriteElementString("pubDate", "Sat, 02 Dec 2006 09:39:58 GMT")
xmlWriter.WriteEndElement
xmlWriter.WriteEndElement
xmlWriter.WriteEndElement
xmlWriter.Flush
msXml.Position = 0
Dim xmlReader As XmlReader = XmlReader.Create(msXml)
While xmlReader.Read
If xmlReader.Name = "rss" Then
Response.Write(xmlReader.ReadOuterXml)
End If
End While
Response.End
msXml.Close
xmlWriter.Close
xmlReader.Close
End Sub
DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米
暂无评论...
更新日志
2024年11月29日
2024年11月29日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]