Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Two ways to return complex data from .NET web methods

0.00/5 (No votes)
7 Mar 2004 1  
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