Solved

Web service method return JSON text without XML tags

Posted on 2009-05-06
2
864 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
This video teaches users how to migrate an existing Wordpress website to a new domain.
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

763 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