Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return XML data from a web service

What is the best way to create a web service that returns a set of x,y coordinates? I am not sure on the object that is the best return type. When consuming the service I want to have it come back as xml preferibly something like this for example:

<TheData>
  <Point>
    <x>0</x>
    <y>2</y>
  </Point>
  <Point>
    <x>5</x>
    <y>3</y>
  </Point>
</TheData>

If someone has a better structure to return please help I am new at all this.

like image 319
Nick LaMarca Avatar asked Jun 08 '10 20:06

Nick LaMarca


People also ask

Can we return XML from REST API?

Data types that REST API can return are as follows: JSON (JavaScript Object Notation) XML. HTML.

How do I send XML data to a restful web service?

If you want to send XML data to the server, set the Request Header correctly to be read by the sever as XML. xmlhttp. setRequestHeader('Content-Type', 'text/xml'); Use the send() method to send the request, along with any XML data.

Which method is used to get the data from a webservice element?

HelloWorld method is the only method for testing the data exchange between the Server and the Browser. We use $. ajax method to send and receive the Server data in JSON format.


2 Answers

Since you are using C#, it is pretty easy. My code is assuming you don't need deserialization, just some XML for a client to parse:

[WebService(Namespace = "http://webservices.mycompany.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
public class PointService : WebService
{
    [WebMethod]
    public Points GetPoints()
    {
        return new Points(new List<Point>
        {
            new Point(0, 2),
            new Point(5, 3)
        });
    }
}

[Serializable]
public sealed class Point
{
    private readonly int x;

    private readonly int y;

    public Point(int x, int y)
    {
        this.x = x;
        this.y = y;
    }

    private Point()
    {
    }

    [XmlAttribute]
    public int X
    {
        get
        {
            return this.x;
        }

        set
        {
        }
    }

    [XmlAttribute]
    public int Y
    {
        get
        {
            return this.y;
        }

        set
        {
        }
    }
}

[Serializable]
[XmlRoot("Points")]
public sealed class Points
{
    private readonly List<Point> points;

    public Points(IEnumerable<Point> points)
    {
        this.points = new List<Point>(points);
    }

    private Points()
    {
    }

    [XmlElement("Point")]
    public List<Point> ThePoints
    {
        get
        {
            return this.points;
        }

        set
        {
        }
    }
}
like image 55
Jesse C. Slicer Avatar answered Oct 25 '22 10:10

Jesse C. Slicer


<Points> <!-- alternatives: PointCollection or PointList -->
  <Point x="0" Y="2" />
  <!-- ... -->
</Points>

Or, you could go for JSON representation instead:

[ { x:0, y:2 }, { x:5, y:10 } ]
like image 29
Franci Penov Avatar answered Oct 25 '22 10:10

Franci Penov