I am following tutorials for a University project and I have hit a bit of a road block, so would really appreciate any insight to help my understanding.
I am trying to write a very simple VB form client / asp.net web service which will allow the client to read some XML from the service.
On the service I have the following code (not included is the code that loads the xml file into service)
Private Function getAllPlayList(ByVal userID As String) As XmlElement
Dim root As XmlElement
Dim xPath As String = "//User[starts-with(@ID, '" & userID & "')]"
root = xmlDataPlaylists.DocumentElement.SelectSingleNode(xPath)
Return root
End Function
<WebMethod()> _
Public Function GetPlaylists(ByVal userID As String) As String
Return getAllPlayList(userID).OuterXml
End Function
Which seems to function when running the service and invoking, which produces:
<string xmlns="http://tempuri.org/"> <User ID="B23785"><Playlist ID="Rock"><Song name="Test Song"><Album>Test Album 1</Album><Artist>Test Artist 1</Artist><Title>Test Song 1</Title><Favourite>False</Favourite></Song><Song name="Test Song 2"><Album>Test Album 2</Album><Artist>Test Artist 2</Artist><Title>Test Song 2</Title><Favourite>False</Favourite></Song></Playlist><Playlist ID="POP"><Song name="Test Song"><Album>Rock Album 1</Album><Artist>Rock Artist 1</Artist><Title>Rock Song 1</Title><Favourite>False</Favourite></Song><Song name="Test Song 2"><Album>Rock Album 2</Album><Artist>Rock Artist 2</Artist><Title>Rock Song 2</Title><Favourite>False</Favourite></Song></Playlist></User> </string>
So as far as that goes, it at least seems to do as intended. The trouble I am having is trying to get the client to receive and load the same XML. The code I have for this is:
Private userID As String
Private playList As Xml.XmlElement
Private service As listService.ServiceSoapClient
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
userID = "B23785"
service = New listService.ServiceSoapClient
getPlaylists()
End Sub
Private Sub getPlaylists()
Dim playListDoc As Xml.XmlDocument
playListDoc = New Xml.XmlDocument()
playListDoc.LoadXml(service.GetPlaylists(userID))
End Sub
The problem seems to be with the line:
playListDoc.LoadXml(service.GetPlaylists(userID))
Which I thought would load into this new XML document the string value returned from the service. Instead I get the error:
The XmlReader must be on a node of type Element instead of a node of type Text
This has me very confused, so if anyone is able to shed some light of why this doesn't work I would be very grateful! Thankyou for your time
service.GetPlaylists(userID)?WebMethodreturn either anXmlElement,XDocument, orXmlDocument. On the surface of it, it sounds wrong, but the serializer handles it properly. If you return XML as a string, it has to escape all the XML brackets and everything which increases the communication size.