SoFunction
Updated on 2025-04-04

Notes on the XmlReader Reader to read the memory stream MemoryStream

MemoryStream object provides methods to create Streams without IO. XmlTextWriter and XmlReader provide methods to quickly write and read XML content. Combined with MemoryStream, you can directly construct XmlTextWriter in memory and read it with XmlReader.

There are two things to note when writing XML using MemoryStream and XmlTextWriter: Operation and reset = 0.

C# 
<%@ Page Language="C#"%> 
<%@ Import Namespace="" %> 
<%@ Import Namespace="" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:///TR/xhtml1/DTD/"> 

<script runat="server"> 
protected void Button1_Click(object sender, EventArgs e) 

(); 
 = "text/xml"; 
MemoryStream msXml = new MemoryStream(); 
XmlTextWriter xmlWriter = new XmlTextWriter(msXml, Encoding.UTF8); 
("rss"); 
("version", "2.0"); 
("channel"); 
("title", "【Meng Xianhui's Wonderful World】");
("link", "/"); 
("description", "NET development technology.");
("language", "zh-CN"); 
("copyright", "Copyright 1999-2007 [Meng Xianhui’s Wonderful World]");
("managingEditor", "amxh[AT]"); 
("image"); 
("title", "【Meng Xianhui's Wonderful World】");
("width", "144"); 
("height", "35"); 
("link", "/"); 
("url", "/Images/"); 
(); 

//Read the database content list in a loop, ignore
//while (()) 
//{ 
// ("item"); 
// ("title", (0)); 
// ("description", (1)); 
// ("link", "/article/a933b187-06c3-4263-9eec-414a54d9c815/"); 
// ("pubDate", (3).ToString("G")); 
// (); 
//} 
//(); 
("item"); 
("title", "Enter direct import of Access database into Excel file in 2.0");
("description", "Enter the Access database directly into an Excel file in 2.0");
("link", "/article/a933b187-06c3-4263-9eec-414a54d9c815/"); 
("pubDate", "Sat, 02 Dec 2006 09:39:58 GMT"); 
(); 
(); 
(); 
(); // Make sure the writer is updated to Stream;
= 0; // Reset the position of the stream so that we can read from scratch
XmlReader xmlReader = (msXml); 
while (()) 

if ( == "rss") 

(()); 


(); 
(); 
(); 
(); 

</script> 

<html xmlns="http:///1999/xhtml"> 
<head runat="server"> 
<title>XmlReader Precautions for the reader to read memory streams MemoryStream</title>
</head> 
<body> 
<form  runat="server"> 
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Read Data" />
</form> 
</body> 
</html> 
 

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) 
 
 = "text/xml" 
Dim msXml As MemoryStream = New MemoryStream 
Dim xmlWriter As XmlTextWriter = New XmlTextWriter(msXml, Encoding.UTF8) 
("rss") 
("version", "2.0") 
("channel") 
("title", "【Meng Xianhui's Wonderful World】")
("link", "/") 
("description", "NET development technology.")
("language", "zh-CN") 
("copyright", "Copyright 1999-2007 [Meng Xianhui's Wonderful World]")
("managingEditor", "amxh[AT]") 
("image") 
("title", "【Meng Xianhui's Wonderful World】")
("width", "144") 
("height", "35") 
("link", "/") 
("url", "/Images/") 
 
("item") 
("title", "Enter the Access database directly into an Excel file in 2.0")
("description", "Enter Access database directly into Excel files in 2.0")
("link", "/article/a933b187-06c3-4263-9eec-414a54d9c815/") 
("pubDate", "Sat, 02 Dec 2006 09:39:58 GMT") 
 
 
 
 
 = 0 
Dim xmlReader As XmlReader = (msXml) 
While  
If  = "rss" Then 
() 
End If 
End While 
 
 
 
 
End Sub