tags:

views:

30

answers:

2

Hi all,

I am not able to get xml response data from .aspx page of VS 2005. Following is the function which writes xml response on the client end:

protected void GetMailContents(double pdblMessageID) { string lstrMailContents = ""; DataSet lobjDs = new DataSet(); StringBuilder stringBuilder = new StringBuilder("");

    lobjDs = mobjCProfileAndMail.GetMailContents(pdblMessageID);

    if (lobjDs != null)
    {
         stringBuilder.Append("<Contents><From>");
         stringBuilder.Append(lobjDs.Tables[0].Rows[0]["From"].ToString());
         stringBuilder.Append("</From><To>");
         stringBuilder.Append(lobjDs.Tables[0].Rows[0]["To"].ToString());
         stringBuilder.Append("</To><Subject>");
         stringBuilder.Append(lobjDs.Tables[0].Rows[0]["Subject"].ToString());
         stringBuilder.Append("</Subject><Message>");
         stringBuilder.Append(lobjDs.Tables[0].Rows[0]["Message"].ToString());
         stringBuilder.Append("</Message></Contents>");
    }

    stringBuilder.Append("</MailContents>");

    lstrMailContents = "<?xml version=\"1.0\" encoding=\"utf-8\"?> \n ";
    lstrMailContents += stringBuilder.ToString();
    Response.ContentEncoding = Encoding.UTF8;
    Response.Write(lstrMailContents);
    Response.End();
}

Code on the client end:

    $(document).ready(function()
    {
        var varURL = document.URL;
        var varArr = varURL.split('=');
        var varMessageID = varArr[1];

        $.get("AjaxData.aspx?Mode=MODALDIALOG."+varMessageID, function(data)
        {
              $(data).find('Contents').each(function()
              {  
                 var varFrom = $(this).find('From').text();  
                 var varTo = $(this).find('To').text();  
                 var varSubject = $(this).find('Subject').text();
                 var varMessage = $(this).find('Message').text(); 
                 alert(varFrom);
             });  

        });
    });

I have written a alert for the data coming from the callback but getting nothing. If I am parsing any fixed xml then its working fine but in case getting response from the .aspx page got nothing. Is there any one who can help me out for this problem.

Thanks.

+1  A: 

First - writing xml through concatenation is really flakey - consider using XmlWriter / XDocument / XmlDocument instead, which will automatically escape any necessary symbols (<, &, etc) rather than result in invalid xml.

Did you clear the response buffer before writing to it? In reality, it would be a lot simpler to do this from a raw handler (ashx) than from within the aspx page life-cycle. Or switch to MVC, which works similarly to the ashx result.

Also - from within the jquery, you should probably speficy the type as xml - see here.

Here's an example of a suitable handler:

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/xml";
        XmlDocument doc = new XmlDocument();
        XmlElement root = (XmlElement) doc.AppendChild(doc.CreateElement("Contents"));
        root.AppendChild(doc.CreateElement("From")).InnerText = "some text";
        root.AppendChild(doc.CreateElement("To")).InnerText = "some more text";
        root.AppendChild(doc.CreateElement("Subject")).InnerText = "this & that";
        root.AppendChild(doc.CreateElement("Message")).InnerText = "a < b > c";
        doc.Save(context.Response.Output);
    }
Marc Gravell
A: 

First off, no need for aspx here - plain IHttpHandler will do and will also be a more natural solution.

As for the question, make sure you clear the output stream before writing out XML and ensure that HTTP headers (specifically Content-Type) are correct. Use Fiddler to see what's going on under the hood.

Anton Gogolev