How to read web service data into XmlDocument in VB

alexcryout
alexcryout used Ask the Experts™
on
Hi experts,

I am new to web service. I try to create a simple application to retrieve data from web service into xmldocument.

Dim WsXmlDoc As XmlDocument = New XmlDocument

Dim oMyService As New WsXfer.SalesXfer
WsXmlDoc.LoadXml(oMyService.GetMySales())

I received the error "Value of type '1-dimentional array' cannot converted to 'String'"
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Without the signature of the webservice, I think that your call should be
WsXmlDoc.LoadXml( oMyService.GetMySales()[0] )

A lot of webservices send back an array with the various out-parameters and function-result.
Carl TawnSenior Systems and Integration Developer

Commented:
It depends what the WebService returns. Odds are it isn't returning pure XML, but some kind of object. You can check this link if you want to get at the raw XML instead:

   http://msdn.microsoft.com/en-us/magazine/cc188761.aspx

Commented:
My friend,

first of all, and since you are new to Web Services, let me tell you that the point of using XML Web Services in .NET is to actually get rid of all the XML handling.

The reason that you cannot do WsXmlDoc.LoadXml(oMyService.GetMySales()) is that the result of oMyService.GetMySales() is not an XML document, but an actual list of Sale objects.

Assuming that the WebService code is this

<WebMethod()> Public Function GetMySales() as Sales()
     'Get the data from the DB
     Dim Res(5) as Sales
     'Fill the output
     Return Res
End Function

Public Class Sales()
     Public ID as integer
     Public Name as string
     Public Amount as double
end Class

Then in your consuming application, you do not need to handle XML. You just say:
Dim mySales() as oMyService.Sales = oMyService.GetMySales()
and you have your data ready.

Now, if you really want (mainly for debugging purposes) to see the actual XML communication going on, then you must "hack" the actual communication. For this, try Carl_Tawn's suggestion above.

If you actually want the web service to return xml, then the WebService method must return the XML string.
<WebMethod()> Public Function GetMySales() as String
     'Build the XML string and return it
End function

Hope this helps.
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

Author

Commented:
Hi Klakkas,

Thanks and it helps me get going here. I used this one and it doesn't work -
Dim mySales() as oMyService.Sales = oMyService.GetMySales()

then I try this one:
Dim oMyService As New WsXfer.SalesXfer
Dim mySales()  = oMyService.GetMySales()

If I already have data in mySales(), how do get data records from here?

Thanks again.
Commented:
You must see what type of object the  oMyService.GetMySales() method returns.

To do that in Visual Studio, go to the line
Dim mySales()  = oMyService.GetMySales()
right click on GetMySales() and select GoTo Definition.

There you will see something like this:
 Public Function GetMySales() As ??????<The type we are looking for>?????

This is the type that the web service will return. So, for example, if you see there
 Public Function GetMySales() As GreatSales()

then your code should be like this

Dim mySales()  As GreatSales = oMyService.GetMySales()

Having completed that, you can normally access your sales from the array, using the properties of the GreatSales object.

For i as integer = 0 to mySales.lenght-1
      listboxSales.items.add(mySales(0).SaleDate)
next

Let me know if you need any more help with that.

Commented:
Just to be clear, before any call to the webService, you always have to instantiate it:
Dim oMyService As New WsXfer.SalesXfer

Author

Commented:
Hi Klakkas,

When I do right click into GetMySales(), I see this
Public Function GetMySales() As Invoice()
            Dim results() As Object = Me.Invoke("GetAllSales", New Object(-1) {})
            Return CType(results(0),Invoice())
End Function

But when I try to define as:
Dim mySales()  As Invoice = oMyService.GetMySales()

I got an error "Type 'Invoice' is not defined"

Thanks

Commented:
simple:

Change
Dim mySales()  As Invoice = oMyService.GetMySales()
To
Dim mySales()  As oMyService.Invoice = oMyService.GetMySales()

and you are done. Now, the mySales array contains Invoices and you can use them as you would normally use a class.

Author

Commented:
Hi klakkas,

I got the same error "Type 'oMyService.Invoice' is not defined"

Commented:
My mistake alex, the type WsXfer.Invoice

Your complete code is this

Dim oMyService As New WsXfer.SalesXfer
Dim mySales()  As WsXfer.Invoice = oMyService.GetMySales()

Let me know how that goes.

Author

Commented:
Hi Klakkas,

It works perfectly this time. Many thanks for your helps.

Author

Commented:
Hi Klakkas,

I hope you don't mind if I have the last question on this subject.

Dim oMyService As New WsXfer.SalesXfer
Dim mySales()  As WsXfer.Invoice = oMyService.GetMySales()

For i as integer = 0 to mySales.lenght-1
      listboxSales.items.add(mySales(0).SaleDate)
next

<Sales>
  <ID>12</ID>
  <Total>123.00</Total>
  <Items>
     <item>
       <ID>345</>
       <Amt>5.00</Amt>
     </Item>
     <item>
       <ID>346</>
       <Amt>12.00</Amt>
     </Item>
   </Items>
</Sales>

And my question is how do I retrieve the "<Item>" data?

Thank you.

Author

Commented:
I got it. Thanks again.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial