熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

XmlReader 讀取器讀取內存流

2022-06-13   來源: .NET編程 
    MemoryStream對象提供了無需進行IO就可以創建Stream的方法XmlTextWriter和XmlReader提供快速書寫和讀取XML內容的方法結合MemoryStream就可以直接在內存中構造XmlTextWriter並用XmlReader進行讀取
  
  使用MemoryStream和XmlTextWriter進行書寫XML需要注意兩點XmlTextWriterFlush操作和重設MemoryStreamPosition =
  
  C#
  
  <%@ Page Language=C#%>
  <%@ Import Namespace=SystemXml %>
  <%@ Import Namespace=SystemIO %>
  <!DOCTYPE html PUBLIC //WC//DTD XHTML Transitional//EN transitionaldtd>
  
  <script runat=server>
   protected void Button_Click(object sender EventArgs e)
   {
   ResponseClear();
   ResponseContentType = text/xml;
   MemoryStream msXml = new MemoryStream();
   XmlTextWriter xmlWriter = new XmlTextWriter(msXml EncodingUTF);
   xmlWriterWriteStartElement(rss);
   xmlWriterWriteAttributeString(version );
   xmlWriterWriteStartElement(channel);
   xmlWriterWriteElementString(title 【孟憲會之精彩世界】);
   xmlWriterWriteElementString(link /Rssaspx);
   xmlWriterWriteElementString(description NET開發技術);
   xmlWriterWriteElementString(language zhCN);
   xmlWriterWriteElementString(copyright Copyright 【孟憲會之精彩世界】);
   xmlWriterWriteElementString(managingEditor amxh[AT]);
   xmlWriterWriteStartElement(image);
   xmlWriterWriteElementString(title 【孟憲會之精彩世界】);
   xmlWriterWriteElementString(width );
   xmlWriterWriteElementString(height );
   xmlWriterWriteElementString(link /);
   xmlWriterWriteElementString(url _//gif);
   xmlWriterWriteEndElement();
  
   //循環讀出數據庫內容列表忽略
   //while (objReaderRead())
   //{
   // xmlWriterWriteStartElement(item);
   // xmlWriterWriteElementString(title objReaderGetString());
   // xmlWriterWriteElementString(description objReaderGetString());
   // xmlWriterWriteElementString(link /article/abceecadc/readaspx);
   // xmlWriterWriteElementString(pubDate objReaderGetDateTime()ToString(G));
   // xmlWriterWriteEndElement();
   //}
   //objReaderClose();
   xmlWriterWriteStartElement(item);
   xmlWriterWriteElementString(title ASPNET 中直接將Access數據庫導入到Excel文件中);
   xmlWriterWriteElementString(description ASPNET 中直接將Access數據庫導入到Excel文件中);
   xmlWriterWriteElementString(link /article/abceecadc/readaspx);
   xmlWriterWriteElementString(pubDate Sat Dec :: GMT);
   xmlWriterWriteEndElement();
   xmlWriterWriteEndElement();
   xmlWriterWriteEndElement();
   xmlWriterFlush(); // 確保書寫器更新到Stream中
   msXmlPosition = ; // 重置流的位置以便我們可以從頭讀取
   XmlReader xmlReader = XmlReaderCreate(msXml);
   while (xmlReaderRead())
   {
   if (xmlReaderName == rss)
   {
   ResponseWrite(xmlReaderReadOuterXml());
   }
   }
   ResponseEnd();
   msXmlClose();
   xmlWriterClose();
   xmlReaderClose();
   }
  </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>
  VBNET
  
  Protected Sub Button_Click(ByVal sender As Object ByVal e As EventArgs)
   ResponseClear
   ResponseContentType = text/xml
   Dim msXml As MemoryStream = New MemoryStream
   Dim xmlWriter As XmlTextWriter = New XmlTextWriter(msXml EncodingUTF)
   xmlWriterWriteStartElement(rss)
   xmlWriterWriteAttributeString(version )
   xmlWriterWriteStartElement(channel)
   xmlWriterWriteElementString(title 【孟憲會之精彩世界】)
   xmlWriterWriteElementString(link /Rssaspx)
   xmlWriterWriteElementString(description NET開發技術)
   xmlWriterWriteElementString(language zhCN)
   xmlWriterWriteElementString(copyright Copyright 【孟憲會之精彩世界】)
   xmlWriterWriteElementString(managingEditor amxh[AT])
   xmlWriterWriteStartElement(image)
   xmlWriterWriteElementString(title 【孟憲會之精彩世界】)
   xmlWriterWriteElementString(width )
   xmlWriterWriteElementString(height )
   xmlWriterWriteElementString(link /)
   xmlWriterWriteElementString(url _//gif)
   xmlWriterWriteEndElement
   xmlWriterWriteStartElement(item)
   xmlWriterWriteElementString(title ASPNET 中直接將Access數據庫導入到Excel文件中)
   xmlWriterWriteElementString(description ASPNET 中直接將Access數據庫導入到Excel文件中)
   xmlWriterWriteElementString(link /article/abceecadc/readaspx)
   xmlWriterWriteElementString(pubDate Sat Dec :: GMT)
   xmlWriterWriteEndElement
   xmlWriterWriteEndElement
   xmlWriterWriteEndElement
   xmlWriterFlush
   msXmlPosition =
   Dim xmlReader As XmlReader = XmlReaderCreate(msXml)
   While xmlReaderRead
   If xmlReaderName = rss Then
   ResponseWrite(xmlReaderReadOuterXml)
   End If
   End While
   ResponseEnd
   msXmlClose
   xmlWriterClose
   xmlReaderClose
  End Sub
From:http://tw.wingwit.com/Article/program/net/201311/12921.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.