Click here to Skip to main content
15,890,557 members
Articles / Web Development / ASP.NET
Article

Two ways to return complex data from .NET web methods

Rate me:
Please Sign up or sign in to vote.
2.25/5 (16 votes)
7 Mar 20043 min read 127.7K   180   16   20
Two ways to return complex data from .net web methods

Why publish an n API to the Internet?

Much like a web page, when you make an API public to the Internet, you allow anyone on the planet to use your web method. Once you post an API you should attempt to keep it up and stable. This does not necessarily mean you grantee how long your API will be available. However, if you wish more application programmers to rely on your API then keep it up and stable. Search engines use this sort of technology to encourage people to automate use of their searching services, some AI Chatbot communities use public API for allowing people to create, train and deploy AI chatbots from anywhere to anywhere in the world, Comic book venders use public APIs to allow anonymous business partners to market their comic books. In short, public APIs make anonymous B2B (Business to Business) relationships more feasible. Not all .NET web methods, however, are used for public APIs, for example, some .NET web methods require passwords or use WSSecurity to limit who can use them. Although non-public API's often use .NET web methods, they are outside the scope of this article.

When to use .NET web methods to publish an API to the Internet

Data can be transferred between remote servants using many architectures. Web methods are faster and easier to implement then writing a socket or .NET remoting server. Web methods more self-documenting then ASPX Request/Response. With web methods, you literally mark your method as a web method and let .NET generate the connectivity mechanism and documentation for accessing the method over the Internet. This way you can quickly get your web method up and running and also benefit from the throttles and security of IIS. The web method approach is not ideal if you cannot afford Microsoft server software because IIS in Windows XP Pro, 2000 pro, etc. has been deliberately disabled to prevent it from being used as a web server on the Internet. If windows 2003 Server, 2000 Server, etc. are in your price range then you will benefit from using .NET web methods. You can, however, practice web method development on any of the .NET enabled Microsoft server and workstation operating systems. If you cannot afford a Microsoft server product then focus on exposing your web methods with .NET socket server architectures, which are just as elegant and expandable but not as ideal for rapid development.

Writing web methods

For this article we focus on two different ways to return complex data types from web methods.

Returning complex data types with .NET serialization/de-serialization

This method just initializes a complex data type with some sample data.

VB.NET
Private Function getDocument() As SComplex
     Dim l_sComplex As SComplex
     l_sComplex = New SComplex()
     l_sComplex.dblVal = 4.14159
     l_sComplex.iVal = 4
     l_sComplex.m_SChildNode.dblChildVal = 4.14159
     l_sComplex.m_SChildNode.iChildVal = 4
     getDocument = l_sComplex
 End Function

This method returns the complex datatype using .NET serialization/de-serialization. You return the object, structure or array is returned and .NET constructs an XML (eXtensible Markup Language) representation.

VB.NET
<WebMethod()> Public Function complex() As Scomplex
Get sample complex data
Dim l_sComplex As SComplex
l_sComplex = getDocument()

Return complex type to be converted into XML in the Response object by .NET

VB.NET
   complex = l_sComplex
End Function

Returning complex data types with manual document creation

This method manually constructs an xml document and returns it. .NET simply appends the xml in that you manually construct to ""<?xml version…" and returns it in the HTTP (HyperText Transfer Protocol) response.

VB.NET
<WebMethod()> Public Function complexManual() As System.Xml.XmlDocument

Get sample complex data

VB.NET
Dim l_sComplex As SComplex
l_sComplex = getDocument()

Manually construct an XML representation of the sample complex data

VB.NET
Dim strData As String
strData = ""
strData = strData & _
     "<SComplex xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" " & _
    "xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" " & _
     "xmlns=""http://tempuri.org/"">"
strData = strData & "   <dblVal>" & l_sComplex.dblVal & "</dblVal> "
strData = strData & " <iVal>" & l_sComplex.iVal & "</iVal> "
strData = strData & " <m_SChildNode>"
strData = strData & "  <dblChildVal>" & _
    l_sComplex.m_SChildNode.dblChildVal & "</dblChildVal> "
strData = strData & "  <iChildVal>" & _
    l_sComplex.m_SChildNode.iChildVal & "</iChildVal> "
strData = strData & " </m_SChildNode>"
strData = strData & "</SComplex>"
Dim pDoc As System.Xml.XmlDocument
pDoc = New System.Xml.XmlDocument()
Return the manually created XML representation of the sample complex data.
VB.NET
    pDoc.LoadXml(strData)
    complexManual = pDoc
End Function

What is returned

Although written differently, both complex() and complexManual() return the same XML representation of the the sample complex data:

XML
<?xml version="1.0" encoding="utf-8" ?> 
<SComplex xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns="http://tempuri.org/">
 <dblVal>4.14159</dblVal> 
 <iVal>4</iVal> 
 <m_SChildNode>
  <dblChildVal>4.14159</dblChildVal> 
  <iChildVal>4</iChildVal> 
 </m_SChildNode>
</SComplex>

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
Keep on coding!

Comments and Discussions

 
Generalplanetapi.com does not exist Pin
Parthasarathy Mandayam23-Oct-07 10:31
Parthasarathy Mandayam23-Oct-07 10:31 
Generalreturning complex data types Pin
eggertj17-Mar-05 5:18
eggertj17-Mar-05 5:18 
GeneralThe worst way of dealing with XML Pin
Daniel Cazzulino [MVP XML]31-May-04 15:30
sussDaniel Cazzulino [MVP XML]31-May-04 15:30 
GeneralTwo ways to return complex data from .NET web methods Pin
dzzxyz31-May-04 19:29
dzzxyz31-May-04 19:29 
GeneralRe: Two ways to return complex data from .NET web methods Pin
Daniel Cazzulino [MVP XML]1-Jun-04 5:18
sussDaniel Cazzulino [MVP XML]1-Jun-04 5:18 
GeneralRe: Two ways to return complex data from .NET web methods Pin
Anonymous1-Jun-04 7:41
Anonymous1-Jun-04 7:41 
GeneralRe: Two ways to return complex data from .NET web methods Pin
dzzxyz1-Jun-04 7:46
dzzxyz1-Jun-04 7:46 
GeneralRe: Two ways to return complex data from .NET web methods Pin
Daniel Cazzulino [XML MVP]1-Jun-04 8:52
Daniel Cazzulino [XML MVP]1-Jun-04 8:52 
"Capitalist driven software", "risk coding unnecessary optimization", oh boy. I think you completely misunderstood me.
It's not about capitalism or requirements timing, it's about doing the things with the appropriate API for the job. Every .NET developer should know that contatenating more than about 10 strings readily justifies switching to the StringBuilder. It's just good programming practice.
You don't need to measure any requirements or be a capitalist monk to know that you don't have to do certain things, i.e. load a huge XML document just to retrieve a couple nodes. It's common sense and good programming practice, that's all.
Want a very concrete example? Mark Fussel (System.Xml PM) discusses the usage of XmlNameTable (http://blogs.msdn.com/mfussell/archive/2004/04/28/121854.aspx). And do you know why he has to do so? Because it's not used in most MSDN documentation, as it's not the primary point being shown, just like your case. The consequence? Most .NET developers completely ignore what's it all about. They are losing about 10% performance straight out of ignorance. Everybody just assumed that the code spread everywhere *not* using the XmlNameTable (because it was showing an XmlTextReader, an XmlDocument, etc.) was the right way to do things.
We should all learn from that. Dogmatic or not, what we write is read and applied (hopefully) by other developers that might not know there are better ways.

The moral: always show the better approach to any problem, even if it's a short example. It's far better to point the reader to further articles explaining what you've ocasionally used but is not the main target of your example.

Daniel Cazzulino
My Weblog

Books:
Beg. C# Web Apps
ASP.NET Components Toolkit
Beg. Web Programming w/VB.NET & VS.NET
Pro ASP.NET Server Controls
GeneralRe: Two ways to return complex data from .NET web methods Pin
dzzxyz1-Jun-04 9:48
dzzxyz1-Jun-04 9:48 
GeneralRe: Two ways to return complex data from .NET web methods Pin
Anonymous1-Jun-04 7:43
Anonymous1-Jun-04 7:43 
GeneralProblem with passing complex types Pin
Meera Rajaram26-May-04 5:12
Meera Rajaram26-May-04 5:12 
Generalthanks Pin
Anonymous7-May-04 8:26
Anonymous7-May-04 8:26 
GeneralThanks for sharing! Pin
Anonymous20-Apr-04 10:33
Anonymous20-Apr-04 10:33 
GeneralI needed this thanks! Pin
Anonymous20-Apr-04 10:33
Anonymous20-Apr-04 10:33 
GeneralHelp with site Pin
dog_spawn10-Apr-04 5:25
dog_spawn10-Apr-04 5:25 
GeneralNice work. Pin
Jason L. Tormlage8-Mar-04 15:43
sussJason L. Tormlage8-Mar-04 15:43 
GeneralWhy publish API's Pin
Mark Focas8-Mar-04 12:52
Mark Focas8-Mar-04 12:52 
GeneralRe: Why publish API's Pin
Anonymous8-Mar-04 15:34
Anonymous8-Mar-04 15:34 
GeneralRe: Why publish API's Pin
Andy Brummer8-Mar-04 18:50
sitebuilderAndy Brummer8-Mar-04 18:50 
GeneralRe: Why publish API's Pin
Anonymous9-Mar-04 6:41
Anonymous9-Mar-04 6:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.