Click here to Skip to main content
13,301,510 members (82,635 online)
Click here to Skip to main content
Add your own
alternative version


16 bookmarked
Posted 7 Mar 2004

Two ways to return complex data from .NET web methods

, 7 Mar 2004
Rate this:
Please Sign up or sign in to vote.
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.

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.

<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

   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.

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

Get sample complex data

Dim l_sComplex As SComplex
l_sComplex = getDocument()

Manually construct an XML representation of the sample complex data

Dim strData As String
strData = ""
strData = strData & _
     "<SComplex xmlns:xsd="""" " & _
    "xmlns:xsi="""" " & _
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.
    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 version="1.0" encoding="utf-8" ?> 
<SComplex xmlns:xsd="" 




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


About the Author

Web Developer
United States United States
Keep on coding!

You may also be interested in...


Comments and Discussions

GeneralProblem with passing complex types Pin
Meera Rajaram26-May-04 6:12
memberMeera Rajaram26-May-04 6:12 
Generalthanks Pin
Anonymous7-May-04 9:26
sussAnonymous7-May-04 9:26 
GeneralThanks for sharing! Pin
Anonymous20-Apr-04 11:33
sussAnonymous20-Apr-04 11:33 
GeneralI needed this thanks! Pin
Anonymous20-Apr-04 11:33
sussAnonymous20-Apr-04 11:33 
GeneralHelp with site Pin
dog_spawn10-Apr-04 6:25
memberdog_spawn10-Apr-04 6:25 
GeneralNice work. Pin
Jason L. Tormlage8-Mar-04 16:43
sussJason L. Tormlage8-Mar-04 16:43 
GeneralWhy publish API's Pin
Mark Focas8-Mar-04 13:52
memberMark Focas8-Mar-04 13:52 
GeneralRe: Why publish API's Pin
Anonymous8-Mar-04 16:34
sussAnonymous8-Mar-04 16:34 
GeneralRe: Why publish API's Pin
andy brummer8-Mar-04 19:50
memberandy brummer8-Mar-04 19:50 
GeneralRe: Why publish API's Pin
Anonymous9-Mar-04 7:41
sussAnonymous9-Mar-04 7: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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171207.1 | Last Updated 8 Mar 2004
Article Copyright 2004 by dzzxyz
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid