title: XslCompiledTransform and Serialization
body: <p>I am trying to implement some functions that will convert one object to another with XslCompiledTransform.</p>
  
  <p>I found some implementations for Serializing an object to XML string and DeSerialize the XML string to an object.</p>
  
  <p>Another function does the XslCompiledTransform from object1 to obejbct2.</p>
  
  <p>To generate the XSLT file i used the Altova MapForce, just loaded the XML of the serialized objects and mapped some attributes.</p>
  
  <p>Now for the problems:</p>
  
  <ul>
  <li>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?</li>
  <li>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.</li>
  </ul>
  
  <p>Here are the functions:</p>
  
  <pre><code>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();
          }
      }
  </code></pre>
  
  <p>Thanks allot,</p>
  
  <p>Omri.</p>