IHttpHandler

S

shapper

Hello,

I am using an ASP.NET 3.5 SP1 and a while ago I created an
IHttpHandler that transforms a Sitemap XML file to a compatible Google
Sitemap (https://www.google.com/webmasters/tools/docs/en/
protocol.html#sitemapXMLExample) XML file using a XSD file.

Now I need to do something similar but I need to create the XML file
from scratch using data that I am getting from an SQL database using
linq.

My problem is how to create the XML file into the stream ... I suppose
this is still the way to do it, right?

My code was done using ASP.NET 2.0.

Public Class SiteMap : Implements IHttpHandler

Public ReadOnly Property IsReusable() As Boolean Implements
IHttpHandler.IsReusable
Get
Return False
End Get
End Property

Public Sub ProcessRequest(ByVal context As HttpContext) Implements
IHttpHandler.ProcessRequest

Dim app As System.Reflection.Assembly =
System.Reflection.Assembly.GetExecutingAssembly

Dim str As Stream = app.GetManifestResourceStream("SiteMap.xsl")

Dim xmlrSiteMap As Xml.XmlReader = Xml.XmlReader.Create(str)

Dim xslGoogle As XslCompiledTransform = New XslCompiledTransform
xslGoogle.Load(xmlrSiteMap)

Dim xpathGoogle As System.Xml.XPath.XPathDocument = New
System.Xml.XPath.XPathDocument(HttpContext.Current.Server.MapPath("SiteMap.xml")))

Dim msGoogle As System.IO.Stream = New System.IO.MemoryStream

Dim googleXslArguments As XsltArgumentList = New
XsltArgumentList
googleXslArguments.AddParam("WebSite.Url", "", "http://" &
ConfigurationManager.AppSettings("WebSite.Url") & "/")

xslGoogle.Transform(xpathGoogle, googleXslArguments, msGoogle)

msGoogle.Flush()
msGoogle.Position = 0

Dim srGoogle As System.IO.StreamReader = New
System.IO.StreamReader(msGoogle)

context.Response.Clear()
context.Response.ContentType = "text/xml; charset=utf-8"
context.Response.Write(srGoogle.ReadToEnd)
context.Response.End()

End Sub ' ProcessRequest

End Class ' SiteMap

Thanks,
Miguel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,739
Latest member
Clint8040

Latest Threads

Top