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
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);
}
}
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)
{
开发者_C百科 //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!
Thank you in advance!!
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>
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>
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>
{
}
Return it as a string and then load that string into an XmlDocument
at the other end.
Is there an issue sending XML via WCF?
Or preferably, create a DataContract
class that mimics the XML structure in code, and then WCF will turn it into XML for you.
精彩评论