By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,548 Members | 1,735 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,548 IT Pros & Developers. It's quick & easy.

reading xml from MemoryStream causes exception

P: n/a

PREVIOUS BACKGROUND POST:

I am trying to reuse a Memory Stream for loading and
transforming the xml it contains

Basically I have defined following interfaces:

Class Render
{

// Memory stream declared inside class so that it is
accessible by all the methods
// It acts as the persistent storage for the life of the
instance of the class
// the point is to have one storage that can be Reloaded w
new XML and re-used

System.IO.MemoryStream xstream = new System.IO.MemoryStream
();

//Constructor

Render(...)
{
.....
//Save XmlDocument to a Memory Stream
XmlDoc.Save(xstream);
- Constructor Render
public Render(XmlReader CleanXmlDoc,string SeriesXSL)

- It Loads 2 things:
the XML
the XSL

and XML is saved to the stream using an XmlDocument to
Load the XmlReader and then save it to a stream

these objects are used by the following Methods using
Xsl.Transform method and other stuff

Methods:

public string RenderSectionPreview()
public string RenderHTML(String RenderMode)
public string RenderPDF(String RenderMode)

I am using

try
{
xstream.Position = 0;
//Console.Write(xstream.Length.ToString());
XpDoc = new XPathDocument(xstream); //<-KEY STATEMENT
}
catch(Exception e)
{
Console.WriteLine ("Exception: {0} - Render HTML -
XPathDoc ", e.ToString());
return e.ToString();
}

inside the RenderHTML

It works one time and renders an HTML file
It errors out the 2nd call at the XPathDocument Statement
due to a MemoryStream error

as shown below:

- Class Call Render - Main **

61600
61600Exception: System.ObjectDisposedException: Cannot
access a closed Stream.
at System.IO.__Error.StreamIsClosed()
at System.IO.MemoryStream.set_Position(Int64 value)
at Luau.Render.Render.RenderHTML(String RenderMode) in
c:\construction\code\r
endering\rendercom\render.cs:line 241 - Render HTML -
XPathDoc
Exception: System.ObjectDisposedException: Cannot access a
closed Stream.
at System.IO.__Error.StreamIsClosed()
at System.IO.MemoryStream.set_Position(Int64 value)
at Luau.Render.Render.RenderHTML(String RenderMode) in
c:\construction\code\r
endering\rendercom\render.cs:line 241 - Render HTML -
XPathDoc

BACKGROUND INFORMATION PREVIOUS POST AND SUGGESTION TO USE
MEMORYSTREAM:
If I understand everything correctly, then you problem is
that you are
re-using the XmlReader that you attached to the
MemoryStream.

An XmlReader is forward only, i.e. you cannot rewind it!
You are on the
right track with using a MemoryStream though. You can
rewind the
MemoryStream by setting Position = 0. Then you can attach
a fresh
Xml(Text)Reader an re-read the stream.

Did I understand your problem and did this help?

--
HTH
Christoph Schittko [MVP]
Software Architect, .NET Mentor

"xmlguy" <xm****@yahoo.com> wrote in message
news:00****************************@phx.gbl...
Cant seem to solve this problem

I need to be able to re-use XmlReader and XPathDocument
for an XSLT Transform.

Basically I have defined following interfaces:

Class Render

(Common and public inside the class)
XPathDocument XpDoc;
XmlReader XrDoc;

- Constructor Render
public Render(XmlReader CleanXmlDoc,string SeriesXSL)

- It Loads 2 things:
the XML
the XSL

these objects are used by the following Methods using
Xsl.Transform method and other stuff

Methods:

public string RenderSectionPreview()
public string RenderHTML(String RenderMode)
public string RenderPDF(String RenderMode)

I was using
XPathDocument XpDoc = new XPathDocument(XmlReader);
inside the constructor and XpDoc was used as source for
XSLT translations in RenderHTML and RenderPDF.

Everything was working fine.

Now I decided to add 2 more methods to allow us to use the same RenderObject (our custom class) and UPDATE the XML by using the following Method ReloadXML.

Also wanted to add a WriteXML object that would allow us
to write the incoming XMLReader (for debug as well as
saving it)

New Methods:

public int ReloadXML(XmlReader CleanXmlDoc1)
public int WriteXML(String RenderMode)

The problem is in this process we are calling the

XPathDocument XpDoc = new XPathDocument(XmlReader);

with CleanXmlDoc1 (the new XML)

again.

We were hoping that the XpDoc would get populated with it
and we could use the XpDoc again as before in the
Xsl.Transform()

ANALYSIS:

After a lot of head breaking have come to the conclusion
that this has something to do with XPathDocument or
XmlReader and the current position of the cursor etc. or
something like that ? BUT I MAY BE COMPLETELY WRONG.
Is it something like this ? What is the issue ?

I tried using MemoryStream to store the XmlReader and then use XPathDocument but logically I am still not able to re- populate the XPathDocument XpDoc with a new XmlReader.

Kindly advise. Thanks.


Nov 11 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
xmlguy wrote:

I am using

try
{
xstream.Position = 0;
//Console.Write(xstream.Length.ToString());
XpDoc = new XPathDocument(xstream); //<-KEY STATEMENT 61600Exception: System.ObjectDisposedException: Cannot
access a closed Stream.


That's not enough to set MemoryStream's position, because the stream is
in closed state after first XmlReader (within XPathDocument constructor)
read it to the end.

You cannot reopen MemoryStream, but you can reuse its internal buffer.
The data within MemoryStream is stored as array of bytes, so to reuse
the XML, which is stored there you have to create new MemoryStream on
that buffer. Someting like

MemoryStream newStream = new MemoryStream(xstream.GetBuffer());
XpDoc = new XPathDocument(newStream);

--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

Nov 11 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.