Click here to Skip to main content
Click here to Skip to main content
Go to top

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=""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.
        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 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

Share

About the Author

dzzxyz
Web Developer
United States United States
Keep on coding!

Comments and Discussions

 
Generalplanetapi.com does not exist PinmemberParthasarathy Mandayam23-Oct-07 10:31 
Generalreturning complex data types Pinmembereggertj17-Mar-05 5:18 
GeneralThe worst way of dealing with XML PinsussDaniel Cazzulino [MVP XML]31-May-04 15:30 
GeneralTwo ways to return complex data from .NET web methods PinmemberDaniel Stephen Rule31-May-04 19:29 
GeneralRe: Two ways to return complex data from .NET web methods PinsussDaniel Cazzulino [MVP XML]1-Jun-04 5:18 
GeneralRe: Two ways to return complex data from .NET web methods PinsussAnonymous1-Jun-04 7:41 
GeneralRe: Two ways to return complex data from .NET web methods PinmemberDaniel Stephen Rule1-Jun-04 7:46 
GeneralRe: Two ways to return complex data from .NET web methods PinmemberDaniel Cazzulino [MVP XML]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 PinmemberDaniel Stephen Rule1-Jun-04 9:48 
GeneralRe: Two ways to return complex data from .NET web methods PinsussAnonymous1-Jun-04 7:43 
GeneralProblem with passing complex types PinmemberMeera Rajaram26-May-04 5:12 
Generalthanks PinsussAnonymous7-May-04 8:26 
GeneralThanks for sharing! PinsussAnonymous20-Apr-04 10:33 
GeneralI needed this thanks! PinsussAnonymous20-Apr-04 10:33 
GeneralHelp with site Pinmemberdog_spawn10-Apr-04 5:25 
GeneralNice work. PinsussJason L. Tormlage8-Mar-04 15:43 
GeneralWhy publish API's PinmemberMark Focas8-Mar-04 12:52 
GeneralRe: Why publish API's PinsussAnonymous8-Mar-04 15:34 
GeneralRe: Why publish API's Pinmemberandy brummer8-Mar-04 18:50 
GeneralRe: Why publish API's PinsussAnonymous9-Mar-04 6:41 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 8 Mar 2004
Article Copyright 2004 by dzzxyz
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid