1

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

5
  • What is the value of the string that is returned when you call service.GetPlaylists(userID)? Commented Nov 26, 2013 at 18:05
  • Sounds like there could be a leading space in that string, hence the Text node. Have you considered NOT serializing to a string? What if your web method just returned the XmlElement, or an XmlDocument. I think WCF could serialize that. Commented Nov 26, 2013 at 18:07
  • @JuanAyala is correct, the most appropriate way to return straight XML from a webservice is to make your WebMethod return either an XmlElement, XDocument, or XmlDocument. 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. Commented Nov 26, 2013 at 18:13
  • Hi, thanks for the suggestions, I tried returning an XML element rather than a string, Now i get a new error: Error 1 Value of type 'System.Xml.Linq.XElement' cannot be converted to 'System.Xml.XmlElement'. Commented Nov 26, 2013 at 19:16
  • Ok, good news! Got it working, had to use this workaround: stackoverflow.com/a/6362584/1850552 Commented Nov 26, 2013 at 19:29

1 Answer 1

1

In a wcf service (vanilla visual studio template) I created this web service

    public XmlElement testme()
    {
        XmlDocument doc = new XmlDocument();
        doc.LoadXml("<foo><bar name=\"hello\"/></foo>");

        string xpath = "//bar";
        XmlElement e = doc.DocumentElement.SelectSingleNode(xpath) as XmlElement;
        return e;
    }

And in a console app I created a vanilla service reference, and invoked the service

        Service1Client c = new Service1Client();
        XElement e = c.testme();

Which pretty much SOAPs it across the wire and translates the XmlElement into XElement, just because XElement is the next incarnation of the MS xml api and thats what the VS web service code generator does.

So you could do that and call it a day, sorry for my C#, I no hablo VB :-D

Sign up to request clarification or add additional context in comments.

1 Comment

This seems to be working, thankyou very much! As I said above though I had to use a workaround to fix another error stackoverflow.com/a/6362584/1850552

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.