XmlReader 读取器读取内存流 MemoryStream 的注意事项
程序员文章站
2023-11-03 22:57:22
memorystream对象提供了无需进行io就可以创建stream的方法,xmltextwriter和xmlreader提供快速书写和读取xml内容的方法,结合memor...
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
上一篇: sys.argv和getopt.getopt()的用法
下一篇: 学习vue.js计算属性