MemoryStream對象提供了無需進行IO就可以創建Stream的方法
XmlTextWriter和XmlReader提供快速書寫和讀取XML內容的方法
結合MemoryStream
就可以直接在內存中構造XmlTextWriter
並用XmlReader進行讀取
使用MemoryStream和XmlTextWriter進行書寫XML
需要注意兩點
XmlTextWriter
Flush操作和重設MemoryStream
Position =
C#
<%@ Page Language=
C#
%>
<%@ Import Namespace=
System
Xml
%>
<%@ Import Namespace=
System
IO
%>
<!DOCTYPE html PUBLIC
//W
C//DTD XHTML
Transitional//EN
transitional
dtd
>
<script runat=
server
>
protected void Button
_Click(object sender
EventArgs e)
{
Response
Clear();
Response
ContentType =
text/xml
;
MemoryStream msXml = new MemoryStream();
XmlTextWriter xmlWriter = new XmlTextWriter(msXml
Encoding
UTF
);
xmlWriter
WriteStartElement(
rss
);
xmlWriter
WriteAttributeString(
version
);
xmlWriter
WriteStartElement(
channel
);
xmlWriter
WriteElementString(
title
【孟憲會之精彩世界】
);
xmlWriter
WriteElementString(
link
/Rss
aspx
);
xmlWriter
WriteElementString(
description
NET開發技術
);
xmlWriter
WriteElementString(
language
zh
CN
);
xmlWriter
WriteElementString(
copyright
Copyright
【孟憲會之精彩世界】
);
xmlWriter
WriteElementString(
managingEditor
amxh[AT]
);
xmlWriter
WriteStartElement(
image
);
xmlWriter
WriteElementString(
title
【孟憲會之精彩世界】
);
xmlWriter
WriteElementString(
width
);
xmlWriter
WriteElementString(
height
);
xmlWriter
WriteElementString(
link
/
);
xmlWriter
WriteElementString(
url
_
/
/
gif
);
xmlWriter
WriteEndElement();
//循環讀出數據庫內容列表
忽略
//while (objReader
Read())
//{
// xmlWriter
WriteStartElement(
item
);
// xmlWriter
WriteElementString(
title
objReader
GetString(
));
// xmlWriter
WriteElementString(
description
objReader
GetString(
));
// xmlWriter
WriteElementString(
link
/article/a
b
c
eec
a
d
c
/read
aspx
);
// xmlWriter
WriteElementString(
pubDate
objReader
GetDateTime(
)
ToString(
G
));
// xmlWriter
WriteEndElement();
//}
//objReader
Close();
xmlWriter
WriteStartElement(
item
);
xmlWriter
WriteElementString(
title
ASP
NET
中直接將Access數據庫導入到Excel文件中
);
xmlWriter
WriteElementString(
description
ASP
NET
中直接將Access數據庫導入到Excel文件中
);
xmlWriter
WriteElementString(
link
/article/a
b
c
eec
a
d
c
/read
aspx
);
xmlWriter
WriteElementString(
pubDate
Sat
Dec
:
:
GMT
);
xmlWriter
WriteEndElement();
xmlWriter
WriteEndElement();
xmlWriter
WriteEndElement();
xmlWriter
Flush(); // 確保書寫器更新到Stream中
msXml
Position =
; // 重置流的位置
以便我們可以從頭讀取
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=
>
<head runat=
server
>
<title>XmlReader 讀取器讀取內存流 MemoryStream 的注意事項</title>
</head>
<body>
<form id=
form
runat=
server
>
<asp:Button ID=
Button
runat=
server
OnClick=
Button
_Click
Text=
讀取數據
/>
</form>
</body>
</html>
VB
NET
Protected Sub Button
_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
UTF
)
xmlWriter
WriteStartElement(
rss
)
xmlWriter
WriteAttributeString(
version
)
xmlWriter
WriteStartElement(
channel
)
xmlWriter
WriteElementString(
title
【孟憲會之精彩世界】
)
xmlWriter
WriteElementString(
link
/Rss
aspx
)
xmlWriter
WriteElementString(
description
NET開發技術
)
xmlWriter
WriteElementString(
language
zh
CN
)
xmlWriter
WriteElementString(
copyright
Copyright
【孟憲會之精彩世界】
)
xmlWriter
WriteElementString(
managingEditor
amxh[AT]
)
xmlWriter
WriteStartElement(
image
)
xmlWriter
WriteElementString(
title
【孟憲會之精彩世界】
)
xmlWriter
WriteElementString(
width
)
xmlWriter
WriteElementString(
height
)
xmlWriter
WriteElementString(
link
/
)
xmlWriter
WriteElementString(
url
_
/
/
gif
)
xmlWriter
WriteEndElement
xmlWriter
WriteStartElement(
item
)
xmlWriter
WriteElementString(
title
ASP
NET
中直接將Access數據庫導入到Excel文件中
)
xmlWriter
WriteElementString(
description
ASP
NET
中直接將Access數據庫導入到Excel文件中
)
xmlWriter
WriteElementString(
link
/article/a
b
c
eec
a
d
c
/read
aspx
)
xmlWriter
WriteElementString(
pubDate
Sat
Dec
:
:
GMT
)
xmlWriter
WriteEndElement
xmlWriter
WriteEndElement
xmlWriter
WriteEndElement
xmlWriter
Flush
msXml
Position =
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
From:http://tw.wingwit.com/Article/program/net/201311/12921.html