1

Script

  $.ajax({
    type: "post",
    url: "Default.aspx?cmd=Setting",
    success: parseXml
  });

function parseXml(xml)
{
   alert(xml);//show Full XML File
  //find every Tutorial and print the author
  $(xml).find("Tutorial").each(function()
  {
    $("#a").append($(this).attr("author") + "<br />");
  });
 }

HTML

<div id="a"></div>

Code

protected void Page_Load(object sender, EventArgs e)
{
    if (Request["cmd"] == "Setting")
    {
        string k=@"<?xml version='1.0' encoding='utf-8' ?>
        <RecentTutorials>
        <Tutorial author='The Reddest'>
        <Title>Silverlight and the Netflix API</Title>
        <Categories>
              <Category>Tutorials</Category>
              <Category>Silverlight 2.0</Category>
              <Category>Silverlight</Category>
              <Category>C#</Category>
              <Category>XAML</Category>
        </Categories>
        <Date>1/13/2009</Date>
        </Tutorial>
        </RecentTutorials>";

          Response.Write(k );
          Response.End();
    }
}

I am a beginner.

This doesn't work.

while alert(xml) show xml File.

3
  • Please define "doesn't work". Commented Mar 13, 2011 at 8:35
  • Have you tried alert(xml); inside parseXml Commented Mar 13, 2011 at 8:39
  • show xml but does not append to div Commented Mar 13, 2011 at 9:03

2 Answers 2

1

Set the proper content type on your server in order to have jQuery automatically parse the XML:

Response.ContentType = "text/xml";
Response.Write(k);
Response.End();

Additionally you could set dataType: 'xml' but that's not necessary if your server is properly configured to send the correct content type.

Here's a live demo.

Sign up to request clarification or add additional context in comments.

Comments

0

Try forcing the dataType to xml: dataType: 'xml'

1 Comment

To be consequent one should use additionally Response.ContentType = "text/xml; charset=utf-8"; in the server code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.