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
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
Well, formatting isn't important.. the question is how does your code look like?
ASKER
Hi Ste5an,
Here is my code:
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
Okay, this is the default serialzation of the dataset. When you want a different structure, then you need to create it.
ASKER
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!.
Then instead of using XmlResult.LoadXml(dsPolLis t.GetXml) you may consider building your response either node by node or you may apply a XML transform on it.
<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
Then instead of using XmlResult.LoadXml(dsPolLis
ASKER
I return:
System.NullReferenceExcept ion: Object reference not set to an instance of an object.
at iWonder_Web_Service.Servic e1.GetPoli cyDetails( ) in C:\Users\JamesL.FRESHINSUR ANCE\Docum ents\Visua l Studio 2008\Projects\iWonder Web Service\iWonder Web Service\Feed.asmx.vb:line 31
When debugging the XMLResult.LoadXML(dsPolLis t.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.FRESHINSUR ANCE\Docum ents\Visua l 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?
System.NullReferenceExcept
at iWonder_Web_Service.Servic
When debugging the XMLResult.LoadXML(dsPolLis
Warning 1 Variable 'XMLResult' is used before it has been assigned a value. A null reference exception could result at runtime. C:\Users\JamesL.FRESHINSUR
Surely the XMLResult.LoadXML is assigning the value by loading the XML?
Yup, a typo. Must be Dim XMLResult As New XmlDocument.
ASKER
Nope
Get the same error.
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ah Ha. Top Man! :)
Now I just need to learn to manipulate the nodes :)
Muchos
Now I just need to learn to manipulate the nodes :)
Muchos