Search code examples
.netxmlashxdirectory

Dynamic XML files using ashx


I'm having an issue where I try to pull the filePath of an XML file using the XML web control. The XML file is stored in a different directory and I am using an ashx file to serve it up. The ashx file works by appending the XML file name to the end of the string.

Dim oXML As New System.Web.UI.WebControls.Xml
oXML.DocumentSource = Server.MapPath("xmlHandler.ashx") & "?xml=sampleXMLfile.xml"
oXML.TransformSource = Server.MapPath("xmlStyles.xslt")
oXML.DataBind()

This doesn't seem to be working for me, and I was wondering if pulling the XML into the code behind is the only way?

Thanks for your help!


Solution

  • MapPath maps a HTTP path/virtual path to a physical folder: http://msdn.microsoft.com/en-us/library/system.web.httpserverutility.mappath.aspx

    So the result, c:\file\on\disk\etc\xmlHandler.ashx?xml=sampleXMLfile.xml, doesn't make sense: it's not a local filename.

    If you want to download that URL, take a look at the WebClient class: http://msdn.microsoft.com/en-us/library/system.net.webclient(v=vs.80).aspx

    ...then set DocumentSource to an XmlDocument loaded from the resulting downloaded data, or a string, or a local file path, as per http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.xml.documentsource(v=vs.80).aspx (As you can see, you can't set DocumentSource to a URL - not that you're providing a valid URL anyway!)