DataContractSerializer and XSLT

Posted by Russ Clark on Stack Overflow See other posts from Stack Overflow or by Russ Clark
Published on 2010-06-08T13:26:13Z Indexed on 2010/06/08 13:32 UTC
Read the original article Hit count: 278

I've got a simple Employee class that I'm trying to serialize to an XDocument and then use XSLT to transform the document to a page that displays both the properties (Name and ID) from the Employee class, and an html form with 2 radio buttons (Approve and Reject) and a submit button. Here is the Employee class:

    [Serializable, DataContract(Namespace="XSLT_MVC.Controllers/")]
    public class Employee
    {
    [DataMember]
    public string Name { get; set; }
    [DataMember]
    public int ID { get; set; }

    public Employee()
    {
    }
    public Employee(string name, int id)
    {
        Name = name;
        ID = id;
    }

    public XDocument GetDoc()
    {
        XDocument doc = new XDocument();

        var serializer = new DataContractSerializer(typeof(Employee));
        using (var writer = doc.CreateWriter())
        {
            serializer.WriteObject(writer, this);
            writer.Close();
        }

        return doc;
    }
    }

And here is the XSLT file:

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >

    <xsl:output method="html" indent="yes"/>

    <xsl:template match="/">
    <html>
    <body>
    <xsl:value-of select="Employee/Name"/>
      <br />
    <xsl:value-of select="Employee/ID"/>
      <br />
    <form method="post" action="/Home/ProcessRequest?id={Employee/ID}">
      <input id="Action" name="Action" type="radio" value="Approved"></input>  Approved     <br />
      <input id="Action" name="Action" type="radio" value="Rejected"></input>  Rejected <br />
      <input type="submit" value="Submit"></input>
    </form>
    </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

When I run this, all I get is the html form with the 2 radio buttons and the submit button, but not the properties from the Employee class. I saw a separate StackOverflow post that said I need to change the <xsl:template match="/"> to match on the namespace of my Employee class like this: <xsl:template match="/XSLT_MVC.Controllers">, but when I do that, now all I get are the Employee properties, and not the html form with the 2 radio buttons and the submit button. Does anyone know what needs to be done so that my transform will select and display both the Employee properties and the html form?

© Stack Overflow or respective owner

Related posts about xslt

Related posts about serialization