Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从web服务返回XML数据_C#_Xml_Web Services - Fatal编程技术网

C# 从web服务返回XML数据

C# 从web服务返回XML数据,c#,xml,web-services,C#,Xml,Web Services,创建返回一组x,y坐标的web服务的最佳方法是什么?我不确定哪个对象是最佳返回类型。在使用该服务时,我希望它以xml的形式返回,最好是这样,例如: <TheData> <Point> <x>0</x> <y>2</y> </Point> <Point> <x>5</x> <y>3</y> </Poin

创建返回一组x,y坐标的web服务的最佳方法是什么?我不确定哪个对象是最佳返回类型。在使用该服务时,我希望它以xml的形式返回,最好是这样,例如:

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

0
2.
5.
3.
如果有人有更好的结构返回,请帮助我,我是新来的。

因为您使用的是C#,所以非常简单。我的代码假设您不需要反序列化,只需要一些XML供客户端解析:

<Points> <!-- alternatives: PointCollection or PointList -->
  <Point x="0" Y="2" />
  <!-- ... -->
</Points>
[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
        {
        }
    }
}
[WebService(命名空间=”http://webservices.mycompany.com/")]
[WebServiceBinding(ConformsTo=WsiProfiles.BasicProfile1_1)]
[ToolboxItem(假)]
公共类PointService:WebService
{
[网络方法]
公共点获取点()
{
返回新点(新列表
{
新点(0,2),
新观点(5,3)
});
}
}
[可序列化]
公共密封类点
{
私有只读INTX;
私有只读INTY;
公共点(整数x,整数y)
{
这个.x=x;
这个。y=y;
}
专用点()
{
}
[XmlAttribute]
公共整数X
{
得到
{
归还这个.x;
}
设置
{
}
}
[XmlAttribute]
公共智力
{
得到
{
把这个还给我;
}
设置
{
}
}
}
[可序列化]
[XmlRoot(“点”)]
公共密封类积分
{
私有只读列表点;
公共点(IEnumerable Points)
{
this.points=新列表(点);
}
私人积分()
{
}
[XmlElement(“点”)]
公开列出要点
{
得到
{
返回此项。点数;
}
设置
{
}
}
}

您能否帮助使用函数签名在web服务中创建此功能?返回哪种类型?一个xml字符串?还是有更好的办法?对不起,尼克,我误解了你的问题。我以为你在问什么是最好的退货方式。如果您需要一个代码示例,@Jesse's是一个很好的示例。谢谢Jesse,最后一件事是有一种简单的方法来发布web服务,以便我可以从任何地方访问吗?拥有空的setters是akward。世界怎么会知道你忽视了他们的任务?为什么不将公共属性改为只读?或者更好:public int X{get;private set;}为了实现最简单的部署,您可以执行所谓的“XCOPY部署”,在该部署中,您的代码只会被复制到网站根目录下,用户可以通过它进行访问。然而,VisualStudio本身(预编译web,部署)中有更健壮的方法将其放到web服务器上。不过,您的环境的里程数可能会有所不同。@Schlaviener,正如我在前言中所说的,这仅用于序列化XML,并且假设不需要反序列化,因此设置器为空。我明白了,所以我必须有一个网站来公开使用。我想也许有一个地方我可以把代码发送到那里,他们会为我发布代码。我没有网站。你是否一直在使用ASMX web服务?如果没有,那么您应该使用WCF。
[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
        {
        }
    }
}