I am currently running a live 'in-memory' XSLT transformation using the following code

XmlDocument XmlDoc = new XmlDocument();
            XmlDoc.LoadXml(DS.GetXml());

            XslCompiledTransform XsltTranformation = new XslCompiledTransform();
            XsltTranformation.Load(@"C:\Users\maskew\Desktop\XSLTMapping.xsl");

            Stream XmlStream = new MemoryStream();
            XmlDoc.Save(XmlStream); //Stream is still blank after this line
            XmlReader XmlRdr = XmlReader.Create(XmlStream);

            MemoryStream stm = new MemoryStream();
            XsltTranformation.Transform(XmlRdr, null, stm);
            stm.Position = 1;

            StreamReader sr = new StreamReader(stm);
            string Output = sr.ReadToEnd();
            Output = Output.Substring(2);
            XmlDoc.LoadXml(Output);

            XmlWriter XmlWrtr = XmlWriter.Create(@"C:\Users\maskew\Desktop\XmlMapping.xml");
            XmlDoc.WriteTo(XmlWrtr);
            XmlWrtr.Flush();
            XmlWrtr.Close();

However, when I move the file from XmlDocument to MemoryStream in line 8 the stream contains nothing when checked and thus stopping the whole program from running.

Does anyone have any idea why this would be occuring?

Solution:

            XDocument XmlDoc = XDocument.Parse(DS.GetXml());

            // Create a writer for writing the transformed file.
            XmlWriter writer = XmlWriter.Create(@"C:\Users\maskew\Desktop\XmlMapping.xml");

            // Create and load the transform with script execution enabled.
            XslCompiledTransform XSLTTransformer = new XslCompiledTransform();
            XsltSettings settings = new XsltSettings();
            settings.EnableScript = true;
            XSLTTransformer.Load(@"C:\Users\maskew\Desktop\XSLTMapping.xsl", settings, null);

            // Execute the transformation.
            XSLTTransformer.Transform(XmlDoc.CreateReader(), writer);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.