我如何以RESTful .NET WCF Web服务返回的XML? [英] How do I return XML in a RESTful .NET WCF Web Service?

查看:124
本文介绍了我如何以RESTful .NET WCF Web服务返回的XML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Visual Web Developer中建立一个WCF Web服务使用4.0框架2010例preSS和将其转换为RESTful服务的使用本教程

I set up a WCF Web Service in Visual Web Developer 2010 Express using the 4.0 Framework and converted it to a RESTful service using this tutorial

我能够把它修改为我喜欢接受URL参数,像这样:

I was able to modify it to my liking to accept url parameters like so:

namespace RestServicePublishing
{
[ServiceContract]
public interface IRestService
{
    [OperationContract(Name="GetXML")]
    [WebGet(UriTemplate = "/{param1}/{param2}")]
    XmlDocument GetXML(string param1, string param2);
}
}

我遇到的问题是,我得到一个类型System.Xml.XmlDocument无法序列化的错误试图返回一个像这样的XML文档时:

The issue I am having is that I am getting a "Type 'System.Xml.XmlDocument' cannot be serialized" error when trying to return an XML document like this:

namespace RestServicePublishing
{
public class RestService : IRestService
    {

    public  XmlDocument GetXML(string param1, string param2)
    {

        //I am not using the parameters currently, I would just like to see if 
        //i can return XML first with this simple example:

        StringBuilder sb = new StringBuilder();
        System.Xml.XmlWriter writer = XmlWriter.Create(sb);
        writer.WriteStartDocument(); 
        writer.WriteStartElement("People");
        writer.WriteStartElement("Person"); 
        writer.WriteAttributeString("Name", "Nick"); 
        writer.WriteEndElement(); 
        writer.WriteStartElement("Person"); 
        writer.WriteStartAttribute("Name"); 
        writer.WriteValue("Nick"); 
        writer.WriteEndAttribute(); 
        writer.WriteEndElement();
        writer.WriteEndElement(); 
        writer.WriteEndDocument(); 
        writer.Flush();

        XmlDocument xmlDocument = new Xml.XmlDocument(); 
        xmlDocument.LoadXml(sb.ToString());
        return xmlDocument; 
    }

}
}

我知道必须有一个更好的方式来建立一个XML文档,并返回它..任何帮助是极大的AP preciated!

I know there has to be a better way to set up an XML document and return it.. Any help is greatly appreciated!

感谢你在前进!

推荐答案

是 - 嗯....型号为WCF说,你不应该试图返回一个XmlDocument本身。相反,你返回的自定义类型的编程环境中定义。这类型需要被标记,以指定应该如何序列化为XML。然后,当该方法返回的自定义类型,WCF序列化为XML文档含蓄。

Yes - well.... the model for WCF says that you should not try to return an XmlDocument itself. Instead you return a custom type defined inside your programming environment. That type needs to be marked up to specify how it should be serialized into XML. Then when that method returns the custom type, WCF serializes it into an XML document implicitly.

我想你想回到什么是这样的:

I think what you want to return is something like this:

<People>
  <Person Name="Nick"/>
  <Person Name="Bonnie"/>
</People>

但DataContractSerializer的不喜欢发出属性。因此,在正常的方式来产生XML Web服务使用WCF,你反而会得到这样的:

But the DataContractSerializer doesn't like to emit attributes. So using WCF in the normal way to produce XML web services, you will instead get something like this:

<People>
  <Person><Name>Nick</Name></Person>
  <Person><Name>Bonnie</Name></Person>
</People>

要做到这一点,写你的C#code是这样的:

To do that, write your C# code like this:

namespace RestServicePublishing
{
    [ServiceContract]
    public interface IRestService
    {
        [OperationContract(Name="GetXML")]
        [WebGet(UriTemplate = "/{param1}/{param2}")]
        List<Person> GetXML(string param1, string param2);
    }
}

那么该类型应该是这样的:

Then the type ought to look like this:

[DataContract]
public class Person
{
    [DataMember]
    public string Name { get; set; }
}

[CollectionDataContract(Name = "People")]
public class People : List<Person>
{
}

这篇关于我如何以RESTful .NET WCF Web服务返回的XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆