Problem. You want to use OutputStream along with XmlWriter. This will write XML directly into your page, which you may require for Google sitemaps or RSS feeds. Solution. Here we see an example of how to use the OutputStream class with the XmlWriter class in the C# programming language.
First, Response.Write is a method in ASP.NET that writes text directly to the page. If you call Response.Write in a code-behind file, the value will be inserted directly into the page.
~~~ Page that uses OutputStream (ASP.NET) ~~~
<%@ Page Language="C#" %>
<script runat="server" type="text/C#">
protected void Page_Load(object sender, EventArgs e)
{
Response.ContentType = "text/xml";
WriteGoogleMap("http://dotnetperls.com/", Response.OutputStream);
Response.End();
return;
}
</script>Example using Google sitemap. Here we make a Google sitemap using some of the above methods and objects. The code replaces the entire .aspx markup stored in the file with the output of code that writes to the Response.
Description. It sets Response.ContentType to "text/xml". You need to do this for XML files like Google Sitemaps or RSS feeds. Internet Explorer can be difficult to deal with unless you do this right. Next, a method called WriteGoogleMap is called and is passed a string and a Stream object.
Using Response.OutputStream. This is a Stream object just like FileStream or many other versions in .NET. It can be written to in the same way. Response.End terminates the page. Response.End() is called to stop any further content from being rendered.
Here is the method that actually writes the XML to the Response.OutputStream. We construct an entire XML document from start to finish. Because the OutputStream is set as the buffer for XmlWriter, when XmlWriter adds elements they will be added to the Response.
--- Method that uses OutputStream (C#) ---
public void WriteGoogleMap(string urlBase, Stream stream)
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Encoding = Encoding.UTF8;
settings.Indent = true;
using (XmlWriter writer = XmlWriter.Create(stream, settings))
{
writer.WriteStartDocument();
writer.WriteStartElement("urlset", "http://www.sitemaps.org/schemas/sitemap/0.9");
// Repeat this code:
writer.WriteStartElement("url");
writer.WriteElementString("loc", "[your url]");
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndDocument();
}
}Description. It is mostly app-specific. Much of the code is application-specific and you don't need to fret about it. What I want to show is that the method receives a Stream, which is the OutputStream of the .aspx file.
Writing to the Stream. The XmlWriter writes to Stream. When we use the XmlWriter to write XML to the page, it is actually added directly to the aspx page.
There is another example of XmlWriter on this site, independent of LINQ and XElement. The tutorial is more likely to be helpful when you have to use XmlWriter in an older application. [C# XmlWriter Tutorial - dotnetperls.com]
Here we used OutputStream and XmlWriter to easily create dynamic XML files in aspx code-behind. It works very well and the XML you create will be interoperable and usable in every web browser and compliant program. We also noted other options you have when using XML in the C# programming language for web pages.