WebService Returns XMLDocument that is a Node

TheWizardofInt

Junior Contributor
Joined
Dec 31, 1969
Messages
333
Location
Orlando, FL
Code:
<WebMethod()> Public Function GetUsers() As XmlDocument
       
        Dim xmlDoc As New XmlDocument
        Dim s As New System.Text.StringBuilder
        Dim sPath As String
        Dim sw As IO.StreamWriter
...
        With s
            .Append("<?xml version=" & """" & "1.0" & """" & "?>" & vbCrLf)
            .Append("<Users>" & vbCrLf)
            If Not dt Is Nothing Then
                For i = 0 To dt.Rows.Count - 1
                    'here we create the entries for all files
                    .Append("<UserName>" & dt.Rows(i).Item(0) & "</UserName>" & vbCrLf)
                Next
            End If
            .Append("</Users>" & vbCrLf)
        End With
        sPath = Server.MapPath("./documents") & "\fhdytxuu.xml"
        sw = New System.IO.StreamWriter(sPath, False)
        sw.Write(s)
        sw.Flush()
        sw.Close()
        sw = Nothing
        xmlDoc.Load(sPath)
        IO.File.Delete(sPath)
        Return xmlDoc

In a nutshell, I use a stringbuilder to build an xml document (which works), I load the XMLDocument (which works), and provide it from the webservice as an XMLDocument

Whatever program reads it, reads it as a node. This is not good, because the people who read it need to read an XML document

Am I using the wrong method here, or leaving a step out to allow my end user to read the document?
 
You need a root node - All XML documents need root nodes...

ie:
Code:
<UserList>
  <Users>
    <UserName>
       User1
    </UserName>
  </Users>
</UserList>

Although, a better XML structure would be something like:
Code:
<UserList>
  <User>
    <UserName>
       User1
    </UserName>
    <FirstName>
       Bob
    </FirstName>
    <LastName>
       Smith
    </LastName>
  </User>
</UserList>

This way you can have multiple values for each user - makes things more flexible if you want to extend the amount of information you store in the file...
 
Web methods return XML. Whatever data type you return is converted to SOAP and resturned.
If you are building the XML yourself that will just become another node in the result document.

What kind of information are you returning and if it has a particular structure could you not define a valid schema for it and let .Net do some of the work?
 
Last edited:
I recreated the node to resember yours, with the root of 'UserList' and the first child of 'Users', then for each username that it is returning, within Users, created a child 'UserName1', 'UserName2', etc

I then write it to an xmldocument using the streamwriter (perhaps I need serialization here?), then load the document just created to an XML document that I instantiate within the subroutine, then return that XMLdocument

The document is valid as a document within the webservice, in that you can read it and do all of the things that you would do to an XML document with it.

I have read on other sites that this is a case of .Net Webervices only returning nodes, not full documents, in which case I think I might just create the document as a file and return that.
 
Back
Top