Wahlin on .NET

Return XML from AJAX-Enabled Web Services

Dan walks you through using the ScriptMethod Attribute.

ASP.NET AJAX-enabled Web services return JSON formatted messages (see my previous articles, here and here) by default since SOAP messages can't be parsed by all browsers.  While converting request and response messages to JSON offers the best cross-browser solution, there may be situations where you need to return XML messages instead of JSON to simplify data processing on the client. For example, a Web service may access RSS data and return it directly to the browser for display in an Intranet scenario where Internet Explorer 5 or higher is used.

By returning XML data, XSLT and XML DOM programming interfaces can be used to transform XML data into other formats with relative ease. Keep in mind that Internet Explorer is currently the only browser to offer robust support for both DOM APIs and XSLT (FireFox has only limited support), so the solution presented in this article is not intended to work across multiple browsers (although that may change as the browsers continue to offer more XML-related features).The solution shown is specific to Internet Explorer 5 or higher deployed in an Intranet environment.

Using the ScriptMethod Attribute
The ASP.NET AJAX Extensions provide a class named ScriptMethodAttribute that can be applied to Web methods in a Web service. By using the ScriptMethodAttribute class, clients can make GET requests instead of POST requests when calling a service and can receive XML data instead of JSON data. ScriptMethodAttribute exposes three properties including UseHttpGet (which accepts a Boolean value), ResponseFormat (which can be set to a ResponseFormat enumeration value), and XmlSerializeString (which can be set to a Boolean value).

I won't discuss the UseHttpGet property in this article, but I do want to mention that you should only change to GET when you're absolutely sure that sensitive data isn't going to be passed to and from a Web service. Sticking with the default POST verb is recommended. An example of using the UseHttpGet property is available in the downloadable code for this article.

When you'd like a Web method to return XML data, you can set the ResponseFormat property to a value of ResponseFormat.Xml as shown below.

 
[ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
[WebMethod]
public XmlElement GetRssFeed(string url)
{
    XmlDocument doc = new XmlDocument();
    doc.Load(url);
    return doc.DocumentElement;
}

In cases where Web methods return string values that you'd like wrapped in an XML tag, you can set the XmlSerializeString property to true as shown below. This will wrap the returned string with <string></string> tags.

[ScriptMethod(ResponseFormat = 
  ResponseFormat.Xml,XmlSerializeString=true)]
[WebMethod]
public string GetXmlString(string input)
{
    return input;
}

XML data returned from a Web service can be processed easily in Internet Explorer 5 or higher by using XSLT stylesheets or DOM APIs. Parsing RSS data returned from a service is fairly straightforward because XPath expressions can be used along with DOM methods to locate <item>, <title>, and <link> tags within the RSS data. The XPath language allows XML documents to be queried to locate desired data in an XML document More information about the XPath language can be found at http://www.w3.org/TR/xpath.

Listing 1 shows an example of calling a Web method that has been marked with the ScriptMethodAttribute class and had its ResponseFormat property set to ResponseFormat.Xml. The returned data is parsed using the client-side selectNodes() and selectSingleNode() methods, which accept XPath expressions. Data extracted from the XML response is written out to the page so that an end user can click on a link to view more details. This example parses my blog here.

The default JSON message format using in ASP.NET AJAX should be leveraged wherever possible when working in a cross-browser environment.  However, in cases where a browser with XML parsing support is available, you can use the ScriptMethodAttribute class to send XML responses to the client.

About the Author

Dan Wahlin (Microsoft MVP for ASP.NET and XML Web Services) is the founder of The Wahlin Group which specializes in .NET and SharePoint onsite, online and video training and consulting solutions. Dan also founded the XML for ASP.NET Developers Web site, which focuses on using ASP.NET, XML, AJAX, Silverlight and Web Services in Microsoft's .NET platform. He's also on the INETA Speaker's Bureau and speaks at conferences and user groups around the world. Dan has written several books on .NET including "Professional Silverlight 2 for ASP.NET Developers," "Professional ASP.NET 3.5 AJAX, ASP.NET 2.0 MVP Hacks and Tips," and "XML for ASP.NET Developers." Read Dan's blog here.

comments powered by Disqus

Featured

  • AI for GitHub Collaboration? Maybe Not So Much

    No doubt GitHub Copilot has been a boon for developers, but AI might not be the best tool for collaboration, according to developers weighing in on a recent social media post from the GitHub team.

  • Visual Studio 2022 Getting VS Code 'Command Palette' Equivalent

    As any Visual Studio Code user knows, the editor's command palette is a powerful tool for getting things done quickly, without having to navigate through menus and dialogs. Now, we learn how an equivalent is coming for Microsoft's flagship Visual Studio IDE, invoked by the same familiar Ctrl+Shift+P keyboard shortcut.

  • .NET 9 Preview 3: 'I've Been Waiting 9 Years for This API!'

    Microsoft's third preview of .NET 9 sees a lot of minor tweaks and fixes with no earth-shaking new functionality, but little things can be important to individual developers.

  • Data Anomaly Detection Using a Neural Autoencoder with C#

    Dr. James McCaffrey of Microsoft Research tackles the process of examining a set of source data to find data items that are different in some way from the majority of the source items.

  • What's New for Python, Java in Visual Studio Code

    Microsoft announced March 2024 updates to its Python and Java extensions for Visual Studio Code, the open source-based, cross-platform code editor that has repeatedly been named the No. 1 tool in major development surveys.

Subscribe on YouTube