views:

721

answers:

3

I have been writing some Web services to be used by a few different client apps and i was trying to write a web service method that simply outputs an RSS XML Feed.

I can create the XML using an XmlTextWriter Object

Then i have tryed outputing to the Response (like i have done in the past when its an aspx page) but this only works it the return type is void (and still doesnt seem to output properly)

Then i tryed making the return type a string and using a StringWriter to output the xml from the XmlTextWriter but the output is then wrapped in a tag.

How can i do this?

A: 

I have some code for this, but it's more than will fit well in an SO post (about 1000 lines). It's really not that hard; the schema is simple enough you can do it yourself, but you don't have to: there are several components you can just plug in to create the xml for you.

You should see this question:
http://stackoverflow.com/questions/57287/asp-net-rss-feed

Joel Coehoorn
I already have created the RSS XML now im looking to get the webservice call to output it directly...
d1k_is
+2  A: 

Obviously create the interfaces and rest of the WCF service as normal.

Mark the class with the following attribute

[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]

And then this function

public Stream GetRSS()
{
 string output;
 //output = some_text;
 MemoryStream ms = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(output));
 WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
 return ms;
}
Jason Coyne
im getting an Object reference error doing with the WebOperationContext.Current.OutgoingResponse.ContentType = "text/html";any ideas?
d1k_is
Try adding the attribute on the class that I marked above.
Jason Coyne
If you still get the error, im not sure. Maybe its the way you are starting the service. This is from a working project of mine.
Jason Coyne
ok, i dont think im using a WCF web service... how do i create 1 of them?
d1k_is
ah. u misread the question. my answer is for a wcf service not an asmx
Jason Coyne
oh ok, what kind of advantages would i get from using a MCF service instead of an amsx? (this early in the development i can still change over easily)
d1k_is
WCF is the new technology for services in .net, so you would get experience in the latest technology. WCF is also more standards compliant, and can be used with multiple protocols, not just HTTP
Jason Coyne
Not only is WCF "new", ASMX is old. It has little life left in it.
John Saunders
A: 

If you must use ASMX, then you can return an XmlDocument. Build the feed XML however you like, but then return the XmlDocument from your web method.

John Saunders