-2

I want to download xml file along with xsl(stylesheet). My code for downloading xml file is as below:

 XPathDocument myXPathDoc = new XPathDocument("myxml.xml");

                XslCompiledTransform myXslTrans = new XslCompiledTransform();
                myXslTrans.Load("myxsl.xsl");

                XmlTextWriter myWriter = new XmlTextWriter("Result.html", null);
                myXslTrans.Transform(myXPathDoc, null, myWriter);

                string strFullPath = Server.MapPath("Result.html");

                string strContents = null;
                System.IO.StreamReader objReader = default(System.IO.StreamReader);
                objReader = new System.IO.StreamReader(strFullPath);
                strContents = objReader.ReadToEnd();
                objReader.Close();

                //attach that XML file and download on local machine
                string attachment = "attachment; filename=" + myWriter;
                Response.ClearContent();
                Response.ContentType = "text/html";
                Response.AddHeader("content-disposition", attachment);
                Response.Write(strContents);

I have searched in google, but not able to find a solution. Give some idea regarding this But its giving exception like The process cannot access the file '~mypath\Result.html' because it is being used by another process.

akeeseth
  • 845
  • 2
  • 15
  • 32

2 Answers2

0

Do you want to write the xml to the response, or the xml with the xsl applied to it? If it is the latter, check this link:

Applying XSLT to XML in C#

If you want to just return the raw XML, then your code seems to be doing that already. However, the title of your question is a little misleading because you indicate you want to download 2 files with 2 request, which may be done with MIME but I fail to see the use of this. If a client is requesting an XML and XSL file, why not just apply them together?

Community
  • 1
  • 1
welegan
  • 3,013
  • 3
  • 15
  • 20
  • Updated the question with your solution, but its giving me exception like 'The process cannot access the file '~myprojectpath\Result.html' because it is being used by another process.'. – akeeseth Jul 30 '13 at 07:27
  • You should wrap `myWriter` in a using() statement or else it will not dispose properly, and probably is keeping `Result.html` stream open. Then, when the reader tries to access it, it is unable to do so. You might also consider instead of writing to `Result.html` file every time, just returning a string from the `XmlWriter` and writing the string to the response stream, your file method is not very scalable. – welegan Jul 30 '13 at 14:56
0

I have added one html file and done like below

 string strFullPathXml = Server.MapPath("myxml.xml");
                    string strFullPathXsl = Server.MapPath("myxsl.xsl");
                    string strFullPathHtml = Server.MapPath("Result.html");

                    XPathDocument xPathDoc = new XPathDocument(strFullPathXml);
                    XslCompiledTransform xslTrans = new XslCompiledTransform();
                    xslTrans.Load(strFullPathXsl);

                    XmlTextWriter xWriter = new XmlTextWriter(strFullPathHtml, null);
                    xslTrans.Transform(xPathDoc, null, xWriter);
                    xWriter.Close();

                    Response.ContentType = "text/html";
                    Response.AppendHeader("Content-Disposition", "attachment; filename=Result.html");
                    Response.ClearContent();
                    Response.WriteFile(strFullPathHtml);
                    Response.Flush();
Response.End();

After that its downloading html file which is in human readable format.

akeeseth
  • 845
  • 2
  • 15
  • 32