xkcd
xkcd

Reputation: 2590

Transforming serialized .net object with XSLT

I have a big .net class and a few xslt files. I'm serializing my object to transform with my xslt files.

My class' name is Application and it has an Applicant property which contains a collection of applications.

public class Application
{
    public Person Applicant { get; set; }
}

public class Person
{
    public List<Application> Applications { get; set; }
}

When I serialize an instance of my class, normally the Xml that I obtained contains z:Ref="i18" attributes to prevent infinite Xml creation to describe existing referenced properties. But this situation changes the required Xpath expressions that I have to write in my Xslt file.

Do I have a chance to serialize my object containing the real entity values instead of z:Ref tags for a specified depth?

Here is my serialization code:

public string Serialize(object input)
{
    XmlDocument XmlDoc = new XmlDocument();
    DataContractSerializer xmlDataContractSerializer = 
        new DataContractSerializer(input.GetType());
    MemoryStream MemStream = new MemoryStream();
    try
    {
        xmlDataContractSerializer.WriteObject(MemStream, input);
        MemStream.Position = 0;
        XmlDoc.Load(MemStream);
        return XmlDoc.InnerXml;
    }
    finally
    {
        MemStream.Close();
    }
}

Thanks in advance,

Anıl

Upvotes: 3

Views: 5961

Answers (2)

Marc Gravell
Marc Gravell

Reputation: 1063198

No, basically. You should, however, be able to use something like:

<xsl:key name="ids" match="*[@z:Id]" use="@z:Id"/>

and then use the xsl key function passing the @z:Ref of the current node, where z is an xmlns alias to http://schemas.microsoft.com/2003/10/Serialization/ - that will at least keep the usage the same throughout.


Full example - xslt first ("my.xslt"):

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/"
  xmlns:dcs="http://schemas.datacontract.org/2004/07/"
>
  <xsl:key name="ids" match="*[@z:Id]" use="@z:Id"/>
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="*[@z:Ref]">
    <xsl:param name="depth" select="5"/>
    <xsl:apply-templates select="key('ids', @z:Ref)">
      <xsl:with-param name="depth" select="$depth"/>
    </xsl:apply-templates>
  </xsl:template>
  <xsl:template match="*[@z:Id]">
    <xsl:param name="depth" select="5"/>    
    <xsl:value-of select="$depth"/>: <xsl:value-of select="name()"/><xsl:text xml:space="preserve">
</xsl:text>
    <xsl:if test="$depth > 0">
      <xsl:apply-templates select="dcs:*">
        <xsl:with-param name="depth" select="($depth)-1"/>
      </xsl:apply-templates>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>

Note that this walks 5 levels via the $depth parameter (decrementing); the key part is the initial match on any element *[@z:Ref], which then uses key to proxy the same request to the original element, as resolved via @z:Id. This means that when we are moving to child elements we just need to use something like:

<xsl:apply-templates select="dcs:*"/>

although we could obviously be more granular, for example:

<xsl:apply-templates select="dcs:Foo"/>

Note also that to add a Foo-specific match, you would add:

<xsl:template match="dcs:Foo[@z:Id]"><!-- --></xsl:template>

to ensure that our *[@z:Ref] match continues to handle reference-forwarding.

And the C#:

using System;
using System.IO;
using System.Runtime.Serialization;
using System.Text;
using System.Xml;
using System.Xml.Xsl;
[DataContract]
public class Foo
{
    [DataMember]
    public Bar Bar { get; set; }
}
[DataContract]
public class Bar
{
    [DataMember]
    public Foo Foo { get; set; }
}
static class Program
{
    static void Main()
    {
        var foo = new Foo();
        var bar = new Bar();
        foo.Bar = bar;
        bar.Foo = foo;
        using (var ms = new MemoryStream())
        {
            var ser = new DataContractSerializer(typeof(Foo), new DataContractSerializerSettings {
                PreserveObjectReferences = true
            });
            ser.WriteObject(ms, foo);
            Console.WriteLine(Encoding.UTF8.GetString(ms.GetBuffer(), 0, (int)ms.Length));
            Console.WriteLine();
            ms.Position = 0;
            var xslt = new XslCompiledTransform();
            xslt.Load("my.xslt");
            using (var reader = XmlReader.Create(ms))
            {
                xslt.Transform(reader, null, Console.Out);
            }
        }
        Console.WriteLine();
        Console.WriteLine("press any key");
        Console.ReadKey();
    }
}

Upvotes: 2

Frank59
Frank59

Reputation: 3261

Try to use XmlSerializer

TestClass TestObj = new TestClass();
XmlSerializer SerializerObj = new XmlSerializer(typeof(TestClass));
TextWriter WriteFileStream = new StreamWriter(@"C:\test.xml");
SerializerObj.Serialize(WriteFileStream, TestObj);
WriteFileStream.Close();

http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx

Upvotes: 0

Related Questions