Filestream, Stream, StreamReader

G

Guest

My situation is simple:
I have an Xml doc that I want to transform into another Xml doc.
I've tested the Xslt in an Xml editor and it works perfectly.
So in C# I create an Xml Document object and load the source xml. Then I
create the XslTransform object and load the xslt. I create a second Xml
document object to store the results of the transform.
There is no need at this stage to write the xml to disk.
The call I'm making to Transform is the one that requires a Stream object as
a parameter.
Here's a small section of the code:
XslTransform transformDoc = new XslTransform();
transformDoc.Load(transformDocumentsPath + xsltFile);

//if the file loaded successfully
if (transformDoc != null){
XmlDocument currentXml = new XmlDocument();
currentXml.Load(document); //document refers to another stream of data
uploaded via a website

if (currentXml != null && currentXml.ChildNodes.Count > 0){
XmlDocument resultXml = new XmlDocument();
FileStream stream = null;

transformDoc.Transform(currentXml.CreateNavigator(),null,stream,null);
resultXml.Load(stream);
return resultXml; }

My problem is with my understanding of how Streams work.

When I run this code I get an error saying that the Stream cannot be null,
which I understand. What I'm not getting though is that streams are for
reading and writing, does this just mean from files written to disk? Or could
I use a stream the way I attempting do it right now?

Thanks
Jacques
 
J

Joerg Jooss

Jacques said:
My situation is simple:
I have an Xml doc that I want to transform into another Xml doc.
I've tested the Xslt in an Xml editor and it works perfectly.
So in C# I create an Xml Document object and load the source xml.
Then I create the XslTransform object and load the xslt. I create a
second Xml document object to store the results of the transform.
There is no need at this stage to write the xml to disk.
The call I'm making to Transform is the one that requires a Stream
object as a parameter. [...]
My problem is with my understanding of how Streams work.

When I run this code I get an error saying that the Stream cannot be
null, which I understand. What I'm not getting though is that streams
are for reading and writing, does this just mean from files written
to disk? Or could I use a stream the way I attempting do it right now?

Jacques,

there are different types of streams -- FileStrams, NetworkStreams,
MemoryStreams. If you don't need to persist your XSLT result at this
point in time, use a MemoryStream to dump the transformation result to
memory. From here, you can construct another XmlDocument, either using
XmlDocument.Load() or an XmlReader that wraps the MemoryStream.

Cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top