Link to home
Start Free TrialLog in
Avatar of Lynchie435
Lynchie435

asked on

Web Service - XML Returning as String and not formatted XML

Morning,

I am attempted to build my first Web Service, now I'm confident with using DataSets and Tables etc. and have managed to at least get my Function to return Data from a SQL Table within my network.

However the data is being returned as one big string rather than formatted XML.

<string xmlns="http://tempuri.org/">
<PolList> <PolList> <Name>Mark Powis tester</Name> <Addr1>Burnt Meadow Road</Addr1> </PolList> <PolList> <Name>Test Powis</Name> <Addr1>Aldwick Road</Addr1> </PolList> <PolList> <Name>Mr M S Powistest</Name> <Addr1>8 This Drive Close</Addr1> </PolList> </PolList>
</string>

Any ideas?

Cheers
Avatar of ste5an
ste5an
Flag of Germany image

Well, formatting isn't important.. the question is how does your code look like?
Avatar of Lynchie435
Lynchie435

ASKER

Hi Ste5an,

Here is my code:


    <WebMethod()> _
    Public Function GetPolicyDetails() As String

        Dim strSQL As String

        strSQL = "SELECT [Name], [Addr1] FROM [InfoCentre].dbo.[ic_yyclient] AS c" & _
                " WHERE [Ref@] = 'POMS01'"

        conn.Open()

        Dim daPolList As New SqlDataAdapter(strSQL, conn)
        Dim dsPolList As New DataSet("PolList")
        Dim tblPolList As DataTable

        daPolList.FillSchema(dsPolList, SchemaType.Source, "PolList")
        daPolList.Fill(dsPolList, "PolList")

        tblPolList = dsPolList.Tables("PolList")

        conn.Close()

        Dim XMLResult = dsPolList.GetXml

        Return XMLResult

    End Function

Open in new window

Okay, this is the default serialzation of the dataset. When you want a different structure, then you need to create it.
Could you point me in the right direction at all? I would be forever grateful.
First of all, return a XML, not a string, See also Rant: Don't return XML in string variables!.

    <WebMethod()> _
    Public Function GetPolicyDetails() As XmlDocument

        Dim strSQL As String

        strSQL = "SELECT [Name], [Addr1] FROM [InfoCentre].dbo.[ic_yyclient] AS c WHERE [Ref@] = 'POMS01'"
        conn.Open()
        Dim daPolList As New SqlDataAdapter(strSQL, conn)
        Dim dsPolList As New DataSet("PolLists")
        Dim tblPolList As DataTable
        daPolList.FillSchema(dsPolList, SchemaType.Source, "PolLists")
        daPolList.Fill(dsPolList, "PolLists")
        tblPolList = dsPolList.Tables("PolList")
        conn.Close()
        Dim XMLResult As XmlDocument
        XmlResult.LoadXml(dsPolList.GetXml)
        Return XMLResult

    End Function

Open in new window


Then instead of using XmlResult.LoadXml(dsPolList.GetXml) you may consider building your response either node by node or you may apply a XML transform on it.
I return:

System.NullReferenceException: Object reference not set to an instance of an object.
   at iWonder_Web_Service.Service1.GetPolicyDetails() in C:\Users\JamesL.FRESHINSURANCE\Documents\Visual Studio 2008\Projects\iWonder Web Service\iWonder Web Service\Feed.asmx.vb:line 31

When debugging the XMLResult.LoadXML(dsPolList.GetXML) I get the following:

Warning      1      Variable 'XMLResult' is used before it has been assigned a value. A null reference exception could result at runtime.      C:\Users\JamesL.FRESHINSURANCE\Documents\Visual Studio 2008\Projects\iWonder Web Service\iWonder Web Service\Feed.asmx.vb      31      9      iWonder Web Service

Surely the XMLResult.LoadXML is assigning the value by loading the XML?
Yup, a typo. Must be Dim XMLResult As New XmlDocument.
Nope

Get the same error.

    <WebMethod()> _
    Public Function GetPolicyDetails() As XmlDocument

        Dim strSQL As String

        strSQL = "SELECT [Name], [Addr1] FROM [InfoCentre].dbo.[ic_yyclient] AS c WHERE [Ref@] = 'POMS01'"
        conn.Open()
        Dim daPolList As New SqlDataAdapter(strSQL, conn)
        Dim dsPolList As New DataSet("PolLists")
        Dim tblPolList As DataTable
        daPolList.FillSchema(dsPolList, SchemaType.Source, "PolLists")
        daPolList.Fill(dsPolList, "PolLists")
        tblPolList = dsPolList.Tables("PolList")
        conn.Close()

        Dim XMLResult As XmlDocument
        XMLResult.LoadXml(dsPolList.GetXml)

        Return XMLResult

    End Function

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ah Ha. Top Man! :)

Now I just need to learn to manipulate the nodes :)

Muchos