加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

.net – 如何在WCF中为[WebGet]方法发出裸XML?

发布时间:2020-12-16 07:41:49 所属栏目:百科 来源:网络整理
导读:如何定义[OperationContract] [WebGet]方法来返回存储在字符串中的 XML,而不使用HTML编码字符串? 该应用程序正在使用WCF服务返回已存储为字符串的XML / XHTML内容. XML通过[DataContract]不对应于任何特定的类.它是由XSLT消费的. [OperationContract][WebGe
如何定义[OperationContract] [WebGet]方法来返回存储在字符串中的 XML,而不使用HTML编码字符串?

该应用程序正在使用WCF服务返回已存储为字符串的XML / XHTML内容. XML通过[DataContract]不对应于任何特定的类.它是由XSLT消费的.

[OperationContract]
[WebGet]
public XmlContent GetContent()
{
   return new XmlContent("<p>given content</p>");
}

我有这个班:

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

但是当序列化时,有一个根标签包装给定的内容.

<XmlContent>
  <p>given content</p>
</XmlContent>

我知道如何更改根标签的名称([XmlRoot(ElementName =“div”)]),但是如果可能,我需要省略根标记.

我也尝试过[DataContract]而不是IXmlSerializable,但似乎不那么灵活.

返回一个XmlElement.你不需要IXmlSerializable.你不需要一个包装类.

示例服务接口:

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

服务实施:

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

另见这个类似的问题,但是没有WebGet的扭曲:
serializing-generic-xml-data-across-wcf-web-service-requests.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读