【问题标题】:How to emit bare XML for a [WebGet] method in WCF?如何为 WCF 中的 [WebGet] 方法发出裸 XML?
【发布时间】:2010-10-30 11:06:09
【问题描述】:

如何定义 [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,但它似乎不太灵活。

【问题讨论】:

    标签: .net wcf xml-serialization


    【解决方案1】:

    返回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

    【讨论】:

    • 谢谢!这可行,但确实有反序列化 XML 然后重新序列化它的缺点。没有我想的那么高效,但至少它消除了根标签。谢谢你的替代方案。 span>
    • 是的,我认为如果您的原始数据存储为字符串,则您必须经历那个多步骤过程。如果它只是静态字符串,您可以只 de-ser 一次,然后缓存结果。假设它是您填写最终文档片段的模板 - 您可以缓存模板并使用每个请求填写动态位。 span>
    • 将此示例与单声道 2.10.8.1 一起使用会引发 - 异常类型“System.Xml.XmlElement”不能将 DataContract 属性名称设置为 null 或空字符串。
    • 我发现返回返回的流工作。 stackoverflow.com/questions/9132281/…
    猜你喜欢
    • 2011-08-19
    • 2011-02-25
    • 2011-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-25
    相关资源
    最近更新 更多