views:

635

answers:

2

Is there anyway to return HTML from a web service in C#? If I have my XSLT transform output tag as XML then I'm geting the XML tag

<?xml version="1.0" encoding="UTF-8"?>

at the top of the returned page. Under that is my HTML and that's fine but what I'd really like to do is change my XSLT transform output tag to HTML and have the web service return just the HTML code. Is this possible?

+1  A: 

I may not have understood your question correctly but would this not be as simple as creating a web method which returns a string value that would contain the HTML?

[WebMethod]
public string GetHTML()
{
    return "<HTML><TITLE>...";
}
James
If I'm not mistaken, I've tried this in the past. When using jQuery AJAX, I believe the response from the ASMX is wrapped in a string tag element and won't render correctly if you try to append it directly to the DOM. You would have to strip any extraneous data that is returned, which seemed kind of messy at the time.
Bit Destroyer
Creating a web method that returns a string value does work but Bit Destroyer is correct in that it's wrapped in a string tag element that tag would have to be stripped off by my JavaScript call. Messy but it would work.
John
A: 

I assume your web service is being called by Javascript for some sort of AJAX-y client-side inclusion.

THIS IS A BAD IDEA

What you want to do is return data to your client-side javascript and use DOM manipulation (i.e. JQuery or ASP.NET AJAX) to insert the data into your page. Do not try to return raw HTML from a web service; that's not the point of a web service! If you need HTML, use an ASPX page to return HTML. If you're using server-side XML transformations to build your HTML, use an ASPX page containing a custom server control that emits the XML transformed into HTML.

Randolpho
The problem with doing that is the project I'm working on has the web side password protected but the web services side is not. So I need a solution that uses a web service to return html.
John