Turn Xml From A Web Service

Returning XML from a Web Service is useful when passing hierarchical data back to a Web Service Client. When passing back XML, you can construct your own XML strings or use the XML framework classes to simplify the construction of the XML. For example, you can build a list of goals along with any important attributes of those goals in an XML document and return this XML from your Web Service.

To return XML from a Web Service, you need to build XML in the Web Service Method. The XML that is built in the Web Service Method can originate from a variety of sources. Your can build XML: a) from scratch by concatenating strings that represent your XML;b) from scratch using the System.XML namespace; c) by loading an existing XML document; d) by transforming an XML document with an XSLT document into a new XML document; or e) by using Microsoft's SQL Server's XML query engine.

This is not an exhaustive list of XML sources, but it gives you a good idea of the possibilities. See pages 146 to 149 for more.

RETURN XML FROM A WEB SERVICE

RETURN XML FROM A WEB SERVICE

D Open the WebServices Template.asmx template from the CD-ROM.

0 Rename the class to XMLWebService.

^0 Add an alias for the System.Xml namespace.

L-Q Add a WebMethod that returns the XmlDocument class.

D Open the WebServices Template.asmx template from the CD-ROM.

0 Rename the class to XMLWebService.

^0 Add an alias for the System.Xml namespace.

L-Q Add a WebMethod that returns the XmlDocument class.

WORK WITH WEB SERVICES

You can load an external XML document using the System.XML namespace. For example, the Web Service below returns all Goals in the goals.xml document. Notice the use of the Server.MapPath function that returns the location of the file on the server.

-0 Create a new XmlDocument variable.

Q Return the XmlDocument 0 Save the file and test the variable. Web Service.

■ The XmlDocument returns from the Web Service.

-0 Create a new XmlDocument variable.

^0 Load an XML string into the XmlDocument.

Q Return the XmlDocument 0 Save the file and test the variable. Web Service.

■ The XmlDocument returns from the Web Service.

Was this article helpful?

0 0

Post a comment