How to return my own XML?

C

Christopher Ambler

When I create a web service, and create a web method that returns a string,
the output is always framed within a tag that looks like:

<string xmlns="http://ewws.example.com/"> </string>

What I'd really like to do is eliminate this string and form my own XML to
send back.

Is this possible?

Christopher
 
K

Kunal Das

Dont know if this is a "best practice", but it works for me, Send it back
via another class. but make sure the secondary class is not the first one
within the namespace, VS.net doesn't like that, it'll say, "The Class
Service1 can be designed, but it is not the first class in the file", so
make sure its something like this:

public class Service1 : System.Web.Services.WebService

{

public Service1()

{

InitializeComponent();

}

[WebMethod]

public URLstr ReturnMyURL()

{

URLstr ustring = new URLstr();

ustring.somestring = "ewws.example.com";

return ustring;

}

public class URLstr

{

public string somestring;

}

Output I got:

<?xml version="1.0" encoding="utf-8" ?>
- <URLstr xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://tempuri.org/">
<somestring>ewws.example.com</somestring>
</URLstr>
Hope this helps.

Kunal
 
R

Rimsky

Create your own public class with the properties that you
want to return and return that class instead of a
string. For example, lets say that your webmethod
returns customer information. Create the public
class "Customer" with properties Name, ID, ... all of
them can be string, int, etc. Then make your webmethod
return your class, not a string. The result will be
something like

<Customer>
<Name>Test Name</Name>
<ID>1</ID>
</Customer>

Hope this helps.
-----Original Message-----
Dont know if this is a "best practice", but it works for me, Send it back
via another class. but make sure the secondary class is not the first one
within the namespace, VS.net doesn't like that, it'll say, "The Class
Service1 can be designed, but it is not the first class in the file", so
make sure its something like this:

public class Service1 : System.Web.Services.WebService

{

public Service1()

{

InitializeComponent();

}

[WebMethod]

public URLstr ReturnMyURL()

{

URLstr ustring = new URLstr();

ustring.somestring = "ewws.example.com";

return ustring;

}

public class URLstr

{

public string somestring;

}

Output I got:

<?xml version="1.0" encoding="utf-8" ?>
- <URLstr xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://tempuri.org/">
<somestring>ewws.example.com</somestring>
</URLstr>
Hope this helps.

Kunal



Christopher Ambler said:
When I create a web service, and create a web method
that returns a
string,
the output is always framed within a tag that looks like:

<string xmlns="http://ewws.example.com/"> </string>

What I'd really like to do is eliminate this string and form my own XML to
send back.

Is this possible?

Christopher


.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top