none
How to add xsl stylesheet node to XML produced by XmlSerializer?

    Question

  • I have an WCF REST service which returns objects serialized with XmlSerializer. How can I add XSL stylesheet information (like the one below) to the output returned by the WCF service?
        <?xml version="1.0" encoding="utf-8"?>
        <?xml-stylesheet type="text/xsl" href="transforms/Customer.xsl"?>
        <Customer>
            <Name>Foo</Name>
        </Customer>
    
    My current operationcontract:
    [OperationContract, XmlSerializerFormat]
    [WebGet( UriTemplate = "{id}" )]
    Customer GetById( string id );
    

    What I would prefer to be able to do, in order to control the stylesheet reference:
    [OperationContract, XmlSerializerFormat]
    [XslStylesheet( Href = "transforms/Customer.xsl" )]
    [WebGet( UriTemplate = "{id}" )]
    Customer GetById( string id );
    

    Johan Andersson
    Tuesday, May 5, 2009 9:33 AM

Answers

All replies

  • You can implement IXmlSerializable on Customer class. However, the XslStyleSheet custom attribute will have to go with Customer class rather than your operation contract so that you can lookup the Href value in IXmlSerializable.WriteXml() method.
    Vivek Desai MCTS (WCF)
    Tuesday, May 5, 2009 10:33 AM
  • Thanks, but I would like to control this based on i.e. the User-Agent (add "_mobile.xsl" for mobile clients and so on).
    Maybe the preferred solution is to separate the entity service from this sort of "presentation logic". For example, I could put a web application in front of this web service?

    But if I would put this in the WCF service, how could I add the node on the "stream level"?

    In order to let a service act differently upon the custom attributes given to an operationcontract, do I need to write a custom binding to do so or is there some else possible feature WCF allows us to extend?
    Johan Andersson
    Tuesday, May 5, 2009 11:22 AM
  • Thats interesting. I agree separating the "presentation logic" from the "business logic" would be the right thing to do. Your web app could be the intermediary that calls WCF service and simply gets the customer object from WCF. The web app can worry about formatting the http response with returned Customer object.

    If you try to do this from your operation, it boils down to having a raw System.IO.Stream as the return type and the operation has to do all the work of creating the return stream as suggested here. Wherever possible, I would prefer to have WCF talk in terms of known objects (e.g. Customer) rather than loose xml.


    Vivek Desai MCTS (WCF)
    Tuesday, May 5, 2009 12:35 PM
  • -> I have an WCF REST service which returns objects serialized with XmlSerializer. How can I add XSL stylesheet information (like the one below) to the output returned by the WCF service?

    Here is the working example I've written a while ago which supports XSL transform both at client (if IE is the client) or server:

    http://oxju0g.blu.livefilestore.com/y1pP362wN633C0nmj4rxcBvNhlV-o_aPVNGMaTIDZzNKdlJGdH4tYe7TfHwDMqJP7W2zko-jhjdTQOdXxMpIBpRVA/SyndicationAndXsltDemo.zip?download

    The raw programming technique Carlos shows is the last resort if you need to use to enable this type of scenario, since you only need to add a simple XML reprocessing instructions into the XML stream rather than having a complete control over the output of the response stream.

    Thanks

    Another Paradigm Shift
    http://shevaspace.blogspot.com
    Thursday, May 7, 2009 6:41 AM
  • Ah, very nice Marco!

    I'm not familiar with the "message encoding level" in WCF but this looks like it's spot on what I need.
    Thank you very much!

    - Johan
    Johan Andersson
    Thursday, May 7, 2009 8:11 AM
  • Marco,

    Do you still have this example available?  I was not able to get to the link you have in your post.

    Thanks
    Carlos

    Wednesday, January 27, 2010 4:23 AM