473,397 Members | 2,116 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

reading xml from MemoryStream causes exception


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
1 11099
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: cnu | last post by:
Hi I have to write images(.gif/.bmp/.jpg/.ico), to db and read them. Uploading images to db works fine for me. Reading from db to byte is also ok. But, when I try to display them in my form...
2
by: D | last post by:
Hi, I am trying to instantiate an instance of XPathDocument after serializing XML into a memory stream. Here is my method that uses XmlSerializer public MemoryStream...
3
by: Nick | last post by:
I have found a class that compresses and uncompresses data but need some help with how to use part of it below is the deflate method which compresses the string that I pass in, this works OK. At...
1
by: Arcnet | last post by:
Using MemoryStream I Have a problem to create a new Image from byte array that originaly was created from an older image (Everything is being preformed in the same Thread) //Get Image...
3
by: T. Davis | last post by:
In C#, I am able to successfully stream a TIFF image that comes from a BLOB field in a database, save it to file, then convert the pages within TIFF file into jpegs (using GDI+) and display on the...
5
by: Naamat | last post by:
Hello, I am the sample FPSEPublish (http://blog.baeke.info/blog/_archives/2005/3/3/393158.html) code to upload a document to Sharepoint (WSS). This works perfectly for samll documents. ...
3
by: apiringmvp | last post by:
All, So I am creating a function that gets a short blurb of html from a blog. I would like to retain all html formating and images. The code below works well, with the exception of one issue....
5
by: sascha.folville | last post by:
Hi, I've some trouble reading the containing frames of a multiframe tiff. First frame is a 1700X2400 px CCITT4, second in JPG 820x1200, third is a CCITT again ... and so on. My function...
3
by: =?Utf-8?B?UGhpbCBKb2huc29u?= | last post by:
Hi, I am using dotnet remoting with a binarry formatter. I have a property that returns a memorystream that has had a file loaded into it. When I try to access this property though I get an...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.