Precautions for XmlReader to read memory stream MemoryStream

Source: Internet
Author: User

The MemoryStream object provides a method to create Stream without IO. XmlTextWriter and XmlReader provide a method to quickly write and read XML content. Combined with MemoryStream, XmlTextWriter can be directly constructed in the memory, read data using XmlReader.

Use MemoryStream and XmlTextWriter to write XML. Pay attention to the following two points: XmlTextWriter. Flush operation and reset 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 button#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 ("edition", "2.0 ");
XmlWriter. WriteStartElement ("channel ");
XmlWriter. WriteElementString ("title", "wonderful world of Meng xianhui ]");
XmlWriter. WriteElementString ("link", "http://dotnet.aspx.cc/Rss.aspx ");
XmlWriter. WriteElementString ("description", "NET development technology. ");
XmlWriter. WriteElementString ("language", "zh-CN ");
XmlWriter. WriteElementString ("copyright", "Copyright [wonderful world of Meng xianhui ]");
XmlWriter. WriteElementString ("managingEditor", "amxh [AT] 21cn.com ");
XmlWriter. WriteStartElement ("image ");
XmlWriter. WriteElementString ("title", "wonderful world of Meng xianhui ]");
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 ();

// Read the database content list cyclically, ignore
// 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", "import the Access database directly to an Excel file in ASP. NET 2.0 ");
XmlWriter. WriteElementString ("description", "import the Access database directly to an Excel file in ASP. NET 2.0 ");
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 (); // ensure that the writer is updated to Stream;
MsXml. Position = 0; // reset the stream Position so that we can read from scratch
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> precautions for XmlReader to read memory stream MemoryStream </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Asp: Button ID = "Button1" runat = "server" OnClick = "button#click" Text = "read data"/>
</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. UTF8)
XmlWriter. WriteStartElement ("rss ")
XmlWriter. WriteAttributeString ("version", "2.0 ")
XmlWriter. WriteStartElement ("channel ")
XmlWriter. WriteElementString ("title", "wonderful world of Meng xianhui ]")
XmlWriter. WriteElementString ("link", "http://dotnet.aspx.cc/Rss.aspx ")
XmlWriter. WriteElementString ("description", "NET development technology. ")
XmlWriter. WriteElementString ("language", "zh-CN ")
XmlWriter. WriteElementString ("copyright", "Copyright [wonderful world of Meng xianhui ]")
XmlWriter. WriteElementString ("managingEditor", "amxh [AT] 21cn.com ")
XmlWriter. WriteStartElement ("image ")
XmlWriter. WriteElementString ("title", "wonderful world of Meng xianhui ]")
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", "import an Access database directly to an Excel file in ASP. NET 2.0 ")
XmlWriter. WriteElementString ("description", "directly import the Access database to an Excel file in ASP. NET 2.0 ")
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.