The answer to you question is - You can't do it in ASP.NET web services !
However, if you build your own listener, instead of using .NET's web services and IIS, you will be able to return what ever you want - but that will make you web service not compatible with the W3C.
The following url is to a sample application shown by Clemens Vasters (http://staff.newtelligence.net/clemensv) in a .NET session held in Israel a few months ago :
If you don't need WDSL self-description and a SOAP-formatted reply why use Web services at all?
Why not build a "plain text service" hosted by IIS that returns plain text using a regular ASP.NET "Web Form" but with all the <head> and <body> junk stripped out of the aspx file and ContentType="text/plain" and Response.Write("Hello World" ) in the code-behind?
Here is one way to avoid the xml tags... perhaps not the best way...:
[WebMethod]
public string HelloWorld()
{
//return "Hello World";
// To avoid having service wrap the response in xml, write it directly then end the response...likely not the best way to do this but it works.
Context.Response.Output.Write("Hello World");
Context.Response.End();
return string.Empty;
}
I was looking to see if there is a more appropriate solution as the data I'm returning is already in xml format, adding another xml tag just confuses the client.. Guess I could strip off its first element but that doesn't strike me as a nice solution either.
Here is one way to avoid the xml tags... perhaps not the best way...:
[WebMethod]
public string HelloWorld()
{
//return "Hello World";
// To avoid having service wrap the response in xml, write it directly then end the response...likely not the best way to do this but it works.
Context.Response.Output.Write("Hello World");
Context.Response.End();
return string.Empty;
}
I was looking to see if there is a more appropriate solution as the data I'm returning is already in xml format, adding another xml tag just confuses the client.. Guess I could strip off its first element but that doesn't strike me as a nice solution either.
Were you aware that ASMX web services are not the current .NET Web Services technology? You are hacking workarounds to make old technology do things it was not designed for. Instead, I strongly suggest you start learning about WCF. Any new features - like
support for REST are much more likely to be made available on WCF than on ASMX.
In fact, I believe you'll find that what you're looking for is already available in WCF.
I think there's a gap in your understanding. It looks like youre writing a [WebMethod] that returns a string and then youre forming a string that looks like XML (not "Hello World") and sending it back, hence your comment about "adding an xml tag" - adding
a tag doesnt confuse most parsers, but sending XML that is expected wrapped in a tag that is not, will break things.
Essentially youre looking for a way to write the string "<?xml version=1.0?><blah><a>b</a></blah>" to the response and are complaining about what is being added, but you have to understand that it is the XML serializer that is adding tags so that the response
string can be represented as a string
Instead you should be making a Blah OBJECT with an A property that serializes itself as you want, and stating THAT OBJECT to be the return type of your method. Thus, the serializer comes along and finds a Blah object being returned (not a preformatted string)
and serializes it accordingly. If you return a string, the serializer will return an xml representation of a string.
I can't think of any way to explain this other than maybe: what is 1 + 2 ?
3
What is "1" + "2" ?
"12"
Strings and numbers here behave differently, within the sematic of the language. In the same way the serialization of returned responses behaves differently; the serializer cannot know that your string contains exactly the xml you want to return any more
than the language can know that you want "1" + "2" to be "3", or that a string of "24-Jul-1990" is your birthday; it just sees text and encodes text in its own way.
If you want encoded text to come out in a certain way, you have to feed data in in a way that will cause it to appear thus. I.e. you have to feed in a
new Blah().a = "b" object so that it will make "<blah><a>b</a></blah>" out of it
ttdbn
Member
15 Points
3 Posts
I want webservice to return plain text without xml tags
Feb 16, 2006 09:18 PM|LINK
I want to have my webservice return "Hello World" without the XML element tags shown below that it returns by default. Thanks in adavnce.
<div class=e> <?xml version="1.0" encoding="utf-8" ?> </div> <div class=e> <div style="MARGIN-LEFT: 1em; TEXT-INDENT: -2em"> <string xmlns="http://tempuri.org/ws1/ws1">Hello World</string> </div></div>idof
Member
473 Points
101 Posts
Re: I want webservice to return plain text without xml tags
Feb 17, 2006 08:16 AM|LINK
Hi,
The answer to you question is - You can't do it in ASP.NET web services !
However, if you build your own listener, instead of using .NET's web services and IIS, you will be able to return what ever you want - but that will make you web service not compatible with the W3C.
The following url is to a sample application shown by Clemens Vasters (http://staff.newtelligence.net/clemensv) in a .NET session held in Israel a few months ago :
http://download.microsoft.com/download/4/3/d/43d173a2-eb4e-49ea-8398-45862d2d4a16/CommTechnologiesDemo.zip
In the above sample, you can find different ways to create a service - one of the ways is by creating a listener.
Best of luck,
Ido.
bnye
Member
136 Points
31 Posts
Re: I want webservice to return plain text without xml tags
Feb 18, 2006 03:28 AM|LINK
Idof -
Thats a bummer. Is there no way to output JSON from a .net webservice? Would something like bitkraft work so that we don't have to reinvent the wheel?
Sincerely,
Ben
sparky62
Member
280 Points
56 Posts
Re: I want webservice to return plain text without xml tags
Feb 18, 2006 09:01 AM|LINK
Why not build a "plain text service" hosted by IIS that returns plain text using a regular ASP.NET "Web Form" but with all the <head> and <body> junk stripped out of the aspx file and ContentType="text/plain" and Response.Write("Hello World" ) in the code-behind?
WesSmith
Member
12 Points
6 Posts
Re: I want webservice to return plain text without xml tags
May 11, 2009 04:07 PM|LINK
[WebMethod]
public string HelloWorld()
{
//return "Hello World";
// To avoid having service wrap the response in xml, write it directly then end the response...likely not the best way to do this but it works.
Context.Response.Output.Write("Hello World");
Context.Response.End();
return string.Empty;
}
I was looking to see if there is a more appropriate solution as the data I'm returning is already in xml format, adding another xml tag just confuses the client.. Guess I could strip off its first element but that doesn't strike me as a nice solution either.
WesSmith
Member
12 Points
6 Posts
Re: I want webservice to return plain text without xml tags
May 11, 2009 04:07 PM|LINK
[WebMethod]
public string HelloWorld()
{
//return "Hello World";
// To avoid having service wrap the response in xml, write it directly then end the response...likely not the best way to do this but it works.
Context.Response.Output.Write("Hello World");
Context.Response.End();
return string.Empty;
}
I was looking to see if there is a more appropriate solution as the data I'm returning is already in xml format, adding another xml tag just confuses the client.. Guess I could strip off its first element but that doesn't strike me as a nice solution either.
johnwsaunder...
Star
11262 Points
1981 Posts
Re: I want webservice to return plain text without xml tags
May 11, 2009 06:09 PM|LINK
Were you aware that ASMX web services are not the current .NET Web Services technology? You are hacking workarounds to make old technology do things it was not designed for. Instead, I strongly suggest you start learning about WCF. Any new features - like support for REST are much more likely to be made available on WCF than on ASMX.
In fact, I believe you'll find that what you're looking for is already available in WCF.
rchesney01
Member
2 Points
1 Post
Re: I want webservice to return plain text without xml tags
Jun 19, 2009 01:08 PM|LINK
How about simply:
[WebMethod]
public void HelloWorld()
{
Context.Response.Output.Write("Hello World");
}
johnwsaunder...
Star
11262 Points
1981 Posts
Re: I want webservice to return plain text without xml tags
Jun 19, 2009 04:40 PM|LINK
I don't know. Did you try it, and did it work?
jcard
Member
6 Points
5 Posts
Re: I want webservice to return plain text without xml tags
Sep 18, 2009 02:31 PM|LINK
I think there's a gap in your understanding. It looks like youre writing a [WebMethod] that returns a string and then youre forming a string that looks like XML (not "Hello World") and sending it back, hence your comment about "adding an xml tag" - adding a tag doesnt confuse most parsers, but sending XML that is expected wrapped in a tag that is not, will break things.
Essentially youre looking for a way to write the string "<?xml version=1.0?><blah><a>b</a></blah>" to the response and are complaining about what is being added, but you have to understand that it is the XML serializer that is adding tags so that the response string can be represented as a string
Instead you should be making a Blah OBJECT with an A property that serializes itself as you want, and stating THAT OBJECT to be the return type of your method. Thus, the serializer comes along and finds a Blah object being returned (not a preformatted string) and serializes it accordingly. If you return a string, the serializer will return an xml representation of a string.
I can't think of any way to explain this other than maybe: what is 1 + 2 ?
3
What is "1" + "2" ?
"12"
Strings and numbers here behave differently, within the sematic of the language. In the same way the serialization of returned responses behaves differently; the serializer cannot know that your string contains exactly the xml you want to return any more than the language can know that you want "1" + "2" to be "3", or that a string of "24-Jul-1990" is your birthday; it just sees text and encodes text in its own way.
If you want encoded text to come out in a certain way, you have to feed data in in a way that will cause it to appear thus. I.e. you have to feed in a new Blah().a = "b" object so that it will make "<blah><a>b</a></blah>" out of it