什么是最好/最灵活的方式有WCF输出XHTML? [英] What is the best / most flexible way to have WCF output XHTML?

查看:139
本文介绍了什么是最好/最灵活的方式有WCF输出XHTML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是最好/最灵活的方式有WCF输出XHTML?如果没有WCF方式(TM)做XHTML输出 - 没有任何常见的工具在那里呢?或者,我需要推出自己的?

What is the best / most flexible way to have WCF output XHTML? If there is no "WCF Way" (tm) to do XHTML output - is there any common tooling out there for it? Or do I need to roll my own?

推荐答案

我不相信有一个官方WCF方式返回XHTML,作为.NET 3.5 SP1。 亚伦勒奇建议有WCF申请在2007年7月一个默认的XSLT样式表,但这样的事情尚未通过WCF团队。

I don't believe there is an "official" WCF way to return XHTML, as of .NET 3.5 SP1. Aaron Lerch suggested having WCF apply a default XSLT stylesheet in July 2007, but something like this hasn't yet been adopted by the WCF team.

至于其他的答案提到的,您可以用样本来自滚你自己的XHTML支持WCF入门套件或使用的描述nofollow的>原始的编程模型。一些 WCF样品code。通过米歇尔·勒鲁布斯塔曼特作为她的 =HTTP的://oreilly.com/catalog/9780596101626/相对=nofollow>学习WCF 本书是如果你决定自己做有益

As other answers noted, you can roll your own XHTML support using samples from the WCF Starter Kit or using the raw programming model described by Carlos Figueira. Some of the WCF sample code provided online by Michele Leroux Bustamante as part of her Learning WCF book are helpful if you decide to do it yourself.

我们返回XHTML以及JSON和XML从我们基于WCF的Web服务。虽然我不知道,我们的方法是的最佳的方式,它的工作。

We return XHTML as well as JSON and XML from our WCF-based web service. While I'm not sure that our approach is the best way, it does work.

我们的做法是使用DataContractSerilizer生成XML,然后申请一个符合XSLT转换,并返回结果流,它现在应该包含XHTML。下面是我们的code的简化版本:

Our approach is to use the DataContractSerilizer to generate XML, then apply a Complied XSLT transform and return the result stream, which should now contain XHTML. Here's a simplified version of our code:

public Stream GetItemAsHtml(string id) {
    Item obj = GetItem(objectId);
    Stream xml = GetXmlStream(obj);    
    return TransformXmlStream(xml, defaultTransform);
}        

public static Stream GetXmlStream(IXmlSerializable item) {
    MemoryStream stream = new MemoryStream();
    using (XmlWriter writer = XmlWriter.Create(stream, new XmlWriterSettings { Encoding = Encoding.UTF8 })) {
        if (writer != null) {
            DataContractSerializer dcs = new DataContractSerializer(item.GetType());
            dcs.WriteObject(writer, item);

            writer.Flush();
            writer.Close();
        }
    }
    stream.Seek(0, SeekOrigin.Begin);
    return stream;
}

public static Stream TransformXmlStream(Stream xml, string xsltFile) {
    XmlReader reader = XmlReader.Create(xml);

    XslCompiledTransform trans = new XslCompiledTransform();
    trans.Load(xsltFile);

    MemoryStream stream = new MemoryStream();
    using (XmlWriter writer = XmlWriter.Create(stream, trans.OutputSettings)) {
        if (writer != null) {
            trans.Transform(reader, writer);

            writer.Flush();
            writer.Close();
        }
    }
    stream.Seek(0, SeekOrigin.Begin);
    return stream;
}

它的工作,但我不认为这是一个情况下WCF的设计者的预期。 :-)

It does the job, but I don't think it's a scenario the designers of WCF anticipated. :-)

这篇关于什么是最好/最灵活的方式有WCF输出XHTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆