Share via


Display XML in aspx page

Question

Monday, January 11, 2010 1:42 PM

 Hi there,

I need a little guidance on how to get XML to display in my aspx page with the appropriate XML start tag "<?xml version="1.0" encoding="utf-8" ?>. Here's my scenario:

I have a stored procedure that is creating my XML with all necessary tags including root element. The stored procedure creates my XML just fine. What I need is to display this XML in the ASPX page when the web request is made using a URL Parameter ?brand=XX. The variable "XX" is being passed to the stored procedure which is retrieving the appropriate XML based on this parameter passed to the stored procedure. I need this XML retrieved into the dataset to be displayed in the ASPX page.

What I'm unsure of is what needs to be in my ASPX page and need to know if my C# is even remotely close to what I need to get this to work. Thanks in advance for any guidance.

Here's my code for the aspx and C# code:

<asp:Xml ID="closingreportxml" runat="server" DocumentSource="~/closingreport.xml"></asp:Xml>

//...and here's my C# code:


protected void Page_Load(object sender, EventArgs e)
    {
        string brand = Request.QueryString["brand"];

        DataSet ds = new DataSet();

        if (Request.QueryString["brand"] != null)
        {

            try
            {
                SqlConnection _sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["Default2"].ConnectionString);
                _sqlConn.Open();
                SqlCommand _sqlCmd = new SqlCommand("usp_Closings_XML", _sqlConn);
                _sqlCmd.CommandType = CommandType.StoredProcedure;
                _sqlCmd.Parameters.Add(new SqlParameter("@brand", brand));

                SqlDataAdapter da = new SqlDataAdapter(_sqlCmd);
                da.Fill(ds, "uvw_Closings");

                ds.WriteXml("closingreport.xml");
            }
            catch
            {
                //
            }
        }         
        

    }

 Also the XML should be displayed as JUST XML when this URL is requested ?brand=XX

 <closings>
<blah></blah>
</closings>

 

 

 

All replies (4)

Tuesday, January 12, 2010 6:33 PM ✅Answered

Which tags you don't need? <NewDataSet>? Then do not use DataSet for formatting xml. Read xml directly from SqlCommand. There are many ways to do it. Here is sample in .NET 3.5 which i haven't tested myself:

using (var xmlReader = _sqlCmd.ExecuteXmlReader())
                {
                    var xDocument = System.Xml.Linq.XDocument.Load(xmlReader);
                    string xml = xDocument.ToString();
                    //if xml is for other systems, do not use Server.HtmlEncode()
                    //and set Mode PassThrough to literal.
                    //if xml does not contain <?xml ...> you can add it manualy
                    XmlText.Text = xml; 
                }

 

 
 

 


Thursday, January 14, 2010 1:45 PM ✅Answered

Thank you very much! That's exactly what I needed to do... use ExecuteXmlReader and add Response.Write("<?xml version=\1.0\ ?>"); in my CS file. I also needed to remove all other tags in my aspx page and set the ContentType to "xml". For everyone elses reference that runs into this, here's my aspx page:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="closingreport.aspx.cs" ContentType="xml" Inherits="closingreport" %>

 
<asp:Literal ID="xmlText" runat="server" Mode="PassThrough"></asp:Literal>

 


Monday, January 11, 2010 4:03 PM

Storing dynamic XML data in file and displaying that file in control is not good idea in multiuser environment or even if there is only one user.

I would try displaying xml in literal control instead of using Xml control.

aspx markup:

<pre>
        <asp:Literal ID="XmlText" runat="server"></asp:Literal>
</pre>

And CS:

//instead of  ds.WriteXml("closingreport.xml"); which writes data for example in
//C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE folder try
using (System.IO.StringWriter sw = new System.IO.StringWriter())
                {
                    ds.WriteXml(sw);
                    string xmlContent = sw.ToString();
                    XmlText.Text = Server.HtmlEncode(xmlContent);   
                }

Monday, January 11, 2010 5:37 PM

Thanks for your reply but this doesn't seem to work for a few reasons, trying the above code gives me this (I've stripped out the data that was in between the XML_F5... tags. It was definitely from my stored procedure but in complete gibberish. I also get extra tags I don't need and there is no proper beginning of an xml document as needed: <?xml version="1.0" encoding="utf-8" ?>

<NewDataSet>
  <uvw_Closings>
    <XML_F52E2B61-18A1-11d1-B105-00805F49916B><closings<XML_F52E2B61-18A1-11d1-B105-00805F49916B>
  </uvw_Closings>
</NewDataSet>

 

Do you have any suggestion on how to do this? By the way I set the Literal mode in the aspx as "PassThrough", is there something else I'm missing which is not causing my literal to show in true xml?

The reason I need my aspx to display as an xml document is so that other websites can read it as a normal xml document and grab the data.

 

Thanks!!