To export xml to a new browser window

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Blacky
    New Member
    • Jan 2009
    • 40

    To export xml to a new browser window

    Hi,

    I want to export a datatable to xml.And open the xml file with a open save cancel dialogue in a new window.Here is the code,

    Code:
           DataTable dt = dtPDF.Copy();
                XmlDocument xmldoc = new XmlDocument();
                dt.TableName = "access";
    
                for (int columnRemoveIndex = 0; columnRemoveIndex <= strRemoveColumn.Length - 1; columnRemoveIndex++)
                {
                    dt.Columns.Remove(strRemoveColumn[columnRemoveIndex]);
                }
    
                for (int headerIndex = 0; headerIndex <= strHeaderText.Length - 1; headerIndex++)
                {
                    dt.Columns[headerIndex].ColumnName = strHeaderText[headerIndex];
                }
                dt.AcceptChanges();
                string FileName = belvaPDFFileName + "_" + DateTime.Now.ToString("mm_dd_yyyy") + DateTime.Now.Millisecond + ".xml";
                string XMLFileName = Folder + "\\" + FileName;
                dt.WriteXml(XMLFileName);
    
                xmldoc.Load(XMLFileName);
    
          
                 HttpContext.Current.Response.ContentType = "application/xml";
                HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + FileName);
                 xmldoc.Save(HttpContext.Current.Response.OutputStream);
                 HttpContext.Current.Response.End();
    The code works fine but it opens xml file in a new tab in the browser.
    I want to open in new window.Any one please help asap.

    Thanks in advance
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    The button or the hyperlink control that opens the new window needs to have JavaScript that does it for you.

    You should have something like:

    VB:
    Code:
    myButton.Attributes.Add("onclick","window.open("""+newAspxPage.aspx"""+","""+"NameOfNewWindow"""+","""+"toolbar=no,menubar=no,width=1000,height=8000,resizable=yes,scrollbars=yes"""+"); return false;")
    C#:
    Code:
    myButton.Attributes.Add("onclick","window.open(\"newAspxPage.aspx\",\"+"NameOfNewWindow\",\"toolbar=no,menubar=no,width=1000,height=8000,resizable=yes,scrollbars=yes\"); return false;");
    See the JavaScript window.open method for more information.

    -Frinny

    Comment

    • Blacky
      New Member
      • Jan 2009
      • 40

      #3
      I tried but i need to open the file in new window.When i used javascript the new window appears as pop up with a blank page n file opens separately....
      whats the change i need to do in my code..??

      Comment

      • Blacky
        New Member
        • Jan 2009
        • 40

        #4
        I tried but i need to open the file in new window.When i used javascript the new window appears as pop up with a blank page n file opens separately....
        whats the change i need to do in my code..??
        choose as best answer reply

        Comment

        • semomaniz
          Recognized Expert New Member
          • Oct 2007
          • 210

          #5
          make sure your new page that is being directed by your JavaScript has the code to display the xml file and also have the appropriate button to save or to cancel

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            Originally posted by Blacky
            I tried but i need to open the file in new window.When i used javascript the new window appears as pop up with a blank page n file opens separately....
            whats the change i need to do in my code..??
            XML is not HTML. The browser is expecting HTML and when it detects that you are sending an XML file it handles it in the most appropriate way: lets the user save the XML file as a download. Or, it may not even do this, it may attempt to display the XML file.

            Instead of sending pure XML down to the browser, wrap the XML into HTML so that it the browser displays it as you want it to be displayed. Once you've done this, be sure to change the Content Type back to HTML so that the browser doesn't treat the HTML as an XML/application type.

            -Frinny

            Comment

            Working...