1

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 6 the stream contains nothing when checked and thus stopping the whole program from running.

Does anyone have any idea why this would be occuring?

UPDATED: The stream is containing information now, however the XmlReader object is receiving none of it still.

2
  • How are you confirming that the stream is empty? Are you checking the length or trying to read from it? Commented Aug 10, 2012 at 8:00
  • Was trying to read from it, close inspection shows the stream containing 700+ chars which then affects my opening question. It is now the line below when moving to the XmlReader that is having the issue and getting nothing into the Reader. Commented Aug 10, 2012 at 8:06

3 Answers 3

3

Try a simplyfying

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.PreserveWhitespace = true;
xmlDoc.LoadXml(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 transform = new XslCompiledTransform();
XsltSettings settings = new XsltSettings();
settings.EnableScript = true;
transform.Load(@"C:\Users\maskew\Desktop\XSLTMapping.xsl", settings, null);

// Execute the transformation.
transform.Transform(xmlDoc, writer);
Sign up to request clarification or add additional context in comments.

3 Comments

I am forced to provide it as a stream due to the xslt removing white space and cannot do this with a compiled XmlDocument, have already tried it.
I updated the code to preserve whitespace, (line 2) have you tried this ?
This works when removing the Whitespace stripping from my XSLT sheet and also changing the .PreserveWhitespace to false to do it instead. - moved the acceptance over as it is a much simpler implementation
1

Try flushing and resetting the stream:

XmlDoc.Save(XmlStream);
XmlStream.Flush();
XmlStream.Position = 0;
XmlReader XmlRdr = XmlReader.Create(XmlStream);

1 Comment

Have done so and on closer inspection the stream is now looking to not be the issue, updated original question.
1
Stream XmlStream = new MemoryStream();

How there can be something in it ? You are constructing an empty memoryStream...

EDIT :

You should try to clarify it by using the 'using' instruction (http://msdn.microsoft.com/fr-fr/library/yh598w02). Basically, class like StreamReader, MemeryStream, etc.. implement the IDisposable interface. If you wrap them with using, it will dispose the object automatically for you.

4 Comments

XmlDoc.Save(XmlStream); Saves the xml document to the stream on the line below that one.
It was on 2.0, changed to 4.0 as I didn't mean to be using 2.0 :)
Have implemented the using instruction also, this has actually fixed the issue, thank you
You should also try to use the XDocument (using System.Xml.Linq) class instead of XmlDocument. XmlDocument is deprecated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.