DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> XML學習教程 >> XML詳解 >> XmlReader 讀取器讀取內存流 MemoryStream 的注意事項
XmlReader 讀取器讀取內存流 MemoryStream 的注意事項
編輯:XML詳解     

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"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>XMLReader 讀取器讀取內存流 MemoryStream 的注意事項</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"></head> <body> <form id="form1" runat="server"> </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", "/School/UploadFiles_7810/201005/20100529214558524.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
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved