如何在RESTful。net WCF Web服务中返回XML ?

时间:2022-10-30 23:18:55

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

我使用4.0框架在Visual Web Developer 2010 Express中设置了一个WCF Web服务,并使用本教程将其转换为RESTful服务

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

我可以根据自己的喜好修改它,接受url参数如下:

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

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:

我的问题是,我得到了一个“类型”System.Xml。当尝试返回这样的XML文档时,XmlDocument'不能被序列化'错误:

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; 
    }

}
}

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

我知道必须有更好的方法来设置XML文档并返回它。非常感谢您的帮助!

Thank you in advance!!

提前谢谢你! !

2 个解决方案

#1


4  

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.

是的,好....WCF的模型说,不应该尝试返回XmlDocument本身。相反,返回在编程环境中定义的自定义类型。需要标记该类型,以指定如何将其序列化为XML。然后,当该方法返回自定义类型时,WCF隐式地将其序列化为XML文档。

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

我认为你想要返回的是这样的:

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

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:

但是DataContractSerializer不喜欢发出属性。因此,以常规的方式使用WCF来生成XML web服务,您将得到如下内容:

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

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

为此,编写c#代码如下:

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>
{
}

#2


2  

Return it as a string and then load that string into an XmlDocument at the other end.

将其作为字符串返回,然后将该字符串加载到另一端的XmlDocument中。

Is there an issue sending XML via WCF?

是否存在通过WCF发送XML的问题?

Or preferably, create a DataContract class that mimics the XML structure in code, and then WCF will turn it into XML for you.

或者,最好创建一个DataContract类,它模仿代码中的XML结构,然后WCF将其转换为XML。

#1


4  

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.

是的,好....WCF的模型说,不应该尝试返回XmlDocument本身。相反,返回在编程环境中定义的自定义类型。需要标记该类型,以指定如何将其序列化为XML。然后,当该方法返回自定义类型时,WCF隐式地将其序列化为XML文档。

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

我认为你想要返回的是这样的:

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

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:

但是DataContractSerializer不喜欢发出属性。因此,以常规的方式使用WCF来生成XML web服务,您将得到如下内容:

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

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

为此,编写c#代码如下:

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>
{
}

#2


2  

Return it as a string and then load that string into an XmlDocument at the other end.

将其作为字符串返回,然后将该字符串加载到另一端的XmlDocument中。

Is there an issue sending XML via WCF?

是否存在通过WCF发送XML的问题?

Or preferably, create a DataContract class that mimics the XML structure in code, and then WCF will turn it into XML for you.

或者,最好创建一个DataContract类,它模仿代码中的XML结构,然后WCF将其转换为XML。