views:

78

answers:

2

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.

A: 
<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 } ]
Franci Penov
Can you help with a function signature to create this in a web service? What type to return? a string of xml? Or is there a better way?
Nick LaMarca
Sorry, @Nick, I misread your question. I thought you were asking on what's the best return format. If you needed a code example, @Jesse's is a good one.
Franci Penov
+1  A: 

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
        {
        }
    }
}
Jesse C. Slicer
Thanks Jesse, one last thing is there a simple way to publish the web service so I can access from anywhere?
Nick LaMarca
Having empty setters is akward. How should the world know that you just ignore their assignment? Why not make the public properties readonly instead? Or even better: public int X {get; private set;}
SchlaWiener
For the easiest deployment, you can do what's known as an "XCOPY deployment" in which your code just gets copied to under the root of your website and users access it through http://yoursite/service.asmx . However, there are more robust methods within Visual Studio itself (precompiled web, deploy) to put it on a web server. Your environment's mileage may vary though.
Jesse C. Slicer
@SchlaWiener, as I said in my preamble, this is for serialized XML only and assumes no deserialization needed, therefore empty setters.
Jesse C. Slicer
I see, so I have to have a website to make it public to use. I thought there might be a place I can just send the code to and they publish it for me. I dont have a website.
Nick LaMarca
Will your code give me raw xml that I can parse in say an iphone app?
Nick LaMarca
@Nick, yes, it will provide good ol' raw XML. It'll look a lot like Franci Penov's first example.
Jesse C. Slicer