Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 如何在WCF中为[WebGet]方法发出裸XML?_.net_Wcf_Xml Serialization - Fatal编程技术网

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

.net 如何在WCF中为[WebGet]方法发出裸XML?,.net,wcf,xml-serialization,.net,Wcf,Xml Serialization,如何定义[OperationContract][WebGet]方法来返回存储在字符串中的XML,而不使用HTML编码字符串 应用程序正在使用WCF服务返回已存储为字符串的XML/XHTML内容。XML不通过[DataContract]对应于任何特定类。它意味着被XSLT使用 [OperationContract] [WebGet] public XmlContent GetContent() { return new XmlContent("<p>given content&l

如何定义[OperationContract][WebGet]方法来返回存储在字符串中的XML,而不使用HTML编码字符串

应用程序正在使用WCF服务返回已存储为字符串的XML/XHTML内容。XML不通过[DataContract]对应于任何特定类。它意味着被XSLT使用

[OperationContract]
[WebGet]
public XmlContent GetContent()
{
   return new XmlContent("<p>given content</p>");
}
但是当序列化时,会有一个根标记包装给定的内容

<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;
        }
    }
}
namespace Cheeso.Samples.Webservices.\u 2009Jun01
{
[ServiceBehavior(Name=“WcfWebGetService”,
Namespace=“urn:Cheeso.Samples.WebServices”,
IncludeExceptionDetailInFaults=true)]
公共类WcfWebGetService:IWebGetService
{
公共元素问候语(字符串问候语)
{
字符串rawXml=“此处填充内容”

”; XmlDocument doc=新的XmlDocument(); doc.Load(new System.IO.StringReader(rawXml)); 返回doc.document元素; } } }
另请参见类似的问题,但没有WebGet扭曲:

.

谢谢!这是可行的,但也有反序列化XML然后重新序列化XML的缺点。没有我希望的那么高效,但至少它消除了根标记。谢谢你的选择。是的,我认为如果你的原始数据是以字符串的形式存储的,你必须经历这个多步骤的过程。如果只是静态字符串,则只能对其进行一次反序列化,然后缓存结果。假设它是您为最终文档片段填充的模板-您可以缓存该模板并在每个请求中填充动态位。将此示例用于mono 2.10.8.1抛出-异常类型“System.Xml.xmlement”不能将DataContract属性名称设置为null或空字符串。在mono上,我发现返回了一个流工作。
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;
        }
    }
}