views:

689

answers:

1

Hello,

I am trying to implement some functions that will convert one object to another with XslCompiledTransform.

I found some implementations for Serializing an object to XML string and DeSerialize the XML string to an object.

Another function does the XslCompiledTransform from object1 to obejbct2.

To generate the XSLT file i used the Altova MapForce, just loaded the XML of the serialized objects and mapped some attributes.

Now for the problems:

  • first I noticed that the XslCompiledTransform doesn't work with XSLT version 2.0. is there any newer functions that do work with XSLT 2.0? maybe some settings?
  • secondly I get an exception when trying to DeSerialize the XML to an object: "There was an error deserializing the object of type myObject Input string was not in a correct format." I don't understand where is the problem. Does anybody have a sample code that does such a thing? all I find in google are Transformations of HTML code and not objects.

Here are the functions:

private static string runXSLT(string xsltFile, string inputXML)
        {
            XmlDocument XmlDoc = new XmlDocument();

            // Load the style sheet.
            XslCompiledTransform xslt = new XslCompiledTransform(true);
            xslt.Load(xsltFile);

            StringReader StrReader = new StringReader(inputXML);
            XmlTextReader XmlReader = new XmlTextReader(StrReader);


            //Create an XmlTextWriter which outputs to memory stream
            Stream stream = new MemoryStream();
            XmlWriter writer = new XmlTextWriter(stream, Encoding.UTF8);

            // Execute the transform and output the results to a file.
            xslt.Transform(XmlReader, writer);

            stream.Position = 0;
            XmlDoc.Load(stream);
            return XmlDoc.InnerXml;
        }

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

        }

    public static Object DeSerializeAnObject(string XmlOfAnObject, Type ObjectType)
    {
        StringReader StrReader = new StringReader(XmlOfAnObject);
        DataContractSerializer xmlDataContractSerializer = new DataContractSerializer(ObjectType);
        XmlTextReader XmlReader = new XmlTextReader(StrReader);
        try
        {
            Object AnObject = xmlDataContractSerializer.ReadObject(XmlReader);
            return AnObject;
        }
        finally
        {
            XmlReader.Close();
            StrReader.Close();
        }
    }

Thanks allot,

Omri.

+2  A: 
  • XslCompiledTransform does not support XSLT 2.0. In fact, XSLT 2.0 is not supported within the .NET Framework at all (you could try the Saxon version for .NET, but be aware that this is just the Java version running inside IKVM).

  • From your description I did not understand why you are taking the detour via XML to convert one object into another. Why don't you simply provide a constructor in your target object that takes your input object as a paramater? Then you can code all the mapping inside that constructor. This is not onlyby far more efficient than serializing, transforming and deserializing your objects you will also get the type safety of C#.

0xA3
+1 for using a constructor to convert the original class to the new class
phsr
The purpose of using the XSL Transformation is to do the mapping of one object to another not hard coded, so I can change the mapped properties without compiling the project.Actually I am building this for mapping data between web services.I get data A from WS 1 and Data B from WS 2, now I have to build a new class to submit to WS 3. The answer class is built from object A + B.Now if WS 1 will return in the future a different object A (A.name changed to A.firstName) I can change the mapping in the XSLT file and do that without compiling the project again.Thanks for the fast reply!