How can I define a [OperationContract] [WebGet] method to return XML that is stored in a string, without HTML encoding the string?
The application is using WCF service to return XML/XHTML content which has been stored as a string. The XML does not correspond to any specific class via [DataContract]. It is meant to be consumed by an XSLT.
[OperationContract]
[WebGet]
public XmlContent GetContent()
{
return new XmlContent("<p>given content</p>");
}
I have this class:
[XmlRoot]
public class XmlContent : IXmlSerializable
{
public XmlContent(string content)
{
this.Content = content;
}
public string Content { get; set; }
#region IXmlSerializable Members
public System.Xml.Schema.XmlSchema GetSchema()
{
return null;
}
public void ReadXml(XmlReader reader)
{
throw new NotImplementedException();
}
public void WriteXml(XmlWriter writer)
{
writer.WriteRaw(this.Content);
}
#endregion
}
But when serialized, there is a root tag the wraps the given content.
<XmlContent>
<p>given content</p>
</XmlContent>
I know how to change the name of the root tag ([XmlRoot(ElementName = "div")]), but I need to omit the root tag, if at all possible.
I have also tried [DataContract] instead of IXmlSerializable, but it seems less flexible.
Return an XmlElement. You don't need IXmlSerializable. You don't need a wrapper class.
example service interface:
namespace Cheeso.Samples.Webservices._2009Jun01
{
[ServiceContract(Namespace="urn:Cheeso.Samples.Webservices" )]
public interface IWebGetService
{
[OperationContract]
[WebGet(
BodyStyle = WebMessageBodyStyle.Bare,
RequestFormat = WebMessageFormat.Xml,
ResponseFormat = WebMessageFormat.Xml,
UriTemplate = "/Greet/{greeting}")]
XmlElement Greet(String greeting);
}
}
service implementation:
namespace Cheeso.Samples.Webservices._2009Jun01
{
[ServiceBehavior(Name="WcfWebGetService",
Namespace="urn:Cheeso.Samples.WebServices",
IncludeExceptionDetailInFaults=true)]
public class WcfWebGetService : IWebGetService
{
public XmlElement Greet (String greeting)
{
string rawXml = "<p>Stuff here</p>";
XmlDocument doc = new XmlDocument();
doc.Load(new System.IO.StringReader(rawXml));
return doc.DocumentElement;
}
}
}
See also, this similar question, but without the WebGet twist:
serializing-generic-xml-data-across-wcf-web-service-requests.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With