Solved

Web service method return JSON text without XML tags

Posted on 2009-05-06
2
858 Views
Last Modified: 2012-05-06
How can I create a webmethod in a web service that returns only text without the XML?  

Here is my code:
public class Services : System.Web.Services.WebService {
    [WebMethod]
    public string Sample()  {
        return "I want only text.";
    }

Current Result:
<?xml version="1.0" encoding="utf-8" ?>
  <string xmlns="http://tempuri.org/">I want only text.</string>

Desired Result:
"I want only text."

Thanks.
0
Comment
Question by:ktmedlin
2 Comments
 
LVL 9

Accepted Solution

by:
tculler earned 500 total points
ID: 24322723
You could do something similar to the following code snippet. There are other ways, but this seems simplest.
static System.String GetInnerText(System.String myFullText)
{
     Int32 start = myFullText.IndexOf('>') + 1;
     return myFullText.Substring(start, myFullText.LastIndexOf('<') - start);
}

Open in new window

0
 

Author Comment

by:ktmedlin
ID: 24332262
Well since ASP.Net web services use SOAP I guess the only way to get a valid JSON string is to strip out the XML as you suggested.  Thanks.
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Grunt script for Build Process 1 42
replica website 2 35
Creating a record in a database with static data from MVC to SQL (Entity) 2 28
exporting html table data 4 26
I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question