我如何以RESTful .NET WCF Web服务返回的XML?NET、RESTful、WCF、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 Framew...

我在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>
{
}
阅读全文

相关推荐

最新文章